赞
踩
目录
Error in **** : non-numeric argument to binary operator
想要生成新的数据列,基于已有数据列的差,但是其中一个作差的数据列不是数值型,不是数值怎么作差,OK出问题了。
- #create data frame
- df <- data.frame(period = c(1, 2, 3, 4, 5, 6, 7, 8),
- sales = c(14, 13, 10, 11, 19, 9, 8, 7),
- returns = c('1', '0', '2', '1', '1', '2', '2', '3'))
-
- #view data frame
- df
-
- #attempt to create new column called 'net'
- df$net <- df$sales - df$returns
#class查看数据类型
- #display class of 'sales' column
- class(df$sales)
-
- [1] "numeric"
-
- #display class of 'returns' column
- class(df$returns)
-
- [1] "character"
#as.numeric转换之后再作差
- #create new column called 'net'
- df$net <- df$sales - as.numeric(df$returns)
-
- #view updated data frame
- df
> #create data frame
> df <- data.frame(period = c(1, 2, 3, 4, 5, 6, 7, 8),
+ sales = c(14, 13, 10, 11, 19, 9, 8, 7),
+ returns = c('1', '0', '2', '1', '1', '2', '2', '3'))
>
> #view data frame
> df
period sales returns
1 1 14 1
2 2 13 0
3 3 10 2
4 4 11 1
5 5 19 1
6 6 9 2
7 7 8 2
8 8 7 3
>
> #attempt to create new column called 'net'
> df$net <- df$sales - df$returns
Error in df$sales - df$returns : non-numeric argument to binary operator
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。