Error in **** : non-numeric argument to binary operator
目录
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', '3')) #view data frame df #attempt to create new column called 'net' df$net <- df$sales - df$returns< code></->
解决:
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 < code></->
完整错误:
create data frame
df
Original: https://blog.csdn.net/zhongkeyuanchongqing/article/details/120623301
Author: Data+Science+Insight
Title: Error in **** : non-numeric argument to binary operator
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/638767/
转载文章受原作者版权保护。转载请注明原作者出处!