当前位置:   article > 正文

Error in **** : non-numeric argument to binary operator_error in x[floor(d)] + x[ceiling(d)] : non-numeric

error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operat

Error in **** : non-numeric argument to binary operator

目录

Error in **** : non-numeric argument to binary operator

问题:

解决:

完整错误:


问题:

想要生成新的数据列,基于已有数据列的差,但是其中一个作差的数据列不是数值型,不是数值怎么作差,OK出问题了。

  1. #create data frame
  2. df <- data.frame(period = c(1, 2, 3, 4, 5, 6, 7, 8),
  3. sales = c(14, 13, 10, 11, 19, 9, 8, 7),
  4. returns = c('1', '0', '2', '1', '1', '2', '2', '3'))
  5. #view data frame
  6. df
  7. #attempt to create new column called 'net'
  8. df$net <- df$sales - df$returns

解决:

#class查看数据类型

  1. #display class of 'sales' column
  2. class(df$sales)
  3. [1] "numeric"
  4. #display class of 'returns' column
  5. class(df$returns)
  6. [1] "character"

#as.numeric转换之后再作差

  1. #create new column called 'net'
  2. df$net <- df$sales - as.numeric(df$returns)
  3. #view updated data frame
  4. 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/880030
推荐阅读
相关标签
  

闽ICP备14008679号