r - : non-numeric argument to binary operator in R错误的原因是什么

标签 r error-handling plot

我试图根据异常数据(+/-)用单独的列进行分组(按年)条形图。
我在R中使用了以下脚本和数据。

mydata <- read.csv("F:/MOD13A1_NDVI_500/Mod_ndvi_500_excel/ndvi_anomaly.csv", head=TRUE)
mydata

        NZ       X2000  X2001  X2002  X2003  X2004  X2005  X2006  X2007  X2008
1 High_mountain  0.007 -0.003 -0.002 -0.016  0.011  0.016 -0.007  0.000 -0.003
2         Taiga -0.002  0.018 -0.006 -0.022  0.018  0.004 -0.016  0.025  0.003
3 Forest_steppe  0.004  0.011 -0.044 -0.008  0.009  0.003 -0.004 -0.005 -0.001
4        Steppe  0.001 -0.016 -0.002  0.007 -0.022 -0.004 -0.017 -0.053  0.000

par(xpd=T, mar=par()$mar+c(0,0,0,6))
barplot(as.matrix(mydata[1:6,]), beside=T)

它返回错误:
Error in -0.01 * height : non-numeric argument to binary operator

这种错误的原因是什么?我在该站点中发现了一些带有二进制运算符错误的非数字参数的问题,但是每种情况都不同。我认为这可能是负(-)值。如何避免这个错误?

最佳答案

它与负值无关。您正在将数字和字符类型混合在一个矩阵中,该矩阵将所有字符掩盖起来。观察

as.matrix(mydata[,1:6])
#   NZ              X2000    X2001    X2002    X2003    X2004   
# 1 "High_mountain" " 0.007" "-0.003" "-0.002" "-0.016" " 0.011"
# 2 "Taiga"         "-0.002" " 0.018" "-0.006" "-0.022" " 0.018"
# 3 "Forest_steppe" " 0.004" " 0.011" "-0.044" "-0.008" " 0.009"
# 4 "Steppe"        " 0.001" "-0.016" "-0.002" " 0.007" "-0.022"

您实际上无法制作具有很多字符值的barplot。尝试省略名字
barplot(as.matrix(mydata[,2:6]), beside=T)

要得到

这是假设您的mydata最终看起来像
mydata<-structure(list(NZ = structure(c(2L, 4L, 1L, 3L), .Label = c("Forest_steppe", 
"High_mountain", "Steppe", "Taiga"), class = "factor"), X2000 = c(0.007, 
-0.002, 0.004, 0.001), X2001 = c(-0.003, 0.018, 0.011, -0.016
), X2002 = c(-0.002, -0.006, -0.044, -0.002), X2003 = c(-0.016, 
-0.022, -0.008, 0.007), X2004 = c(0.011, 0.018, 0.009, -0.022
), X2005 = c(0.016, 0.004, 0.003, -0.004), X2006 = c(-0.007, 
-0.016, -0.004, -0.017), X2007 = c(0, 0.025, -0.005, -0.053), 
    X2008 = c(-0.003, 0.003, -0.001, 0)), .Names = c("NZ", "X2000", 
"X2001", "X2002", "X2003", "X2004", "X2005", "X2006", "X2007", 
"X2008"), class = "data.frame", row.names = c("1", "2", "3", 
"4"))

关于r - : non-numeric argument to binary operator in R错误的原因是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25944927/

相关文章:

xml - 抓取分层数据

php - PHP错误字符串

node.js - 如何将 react 错误发送到服务器并记录它?

r - R 中灵活的对角线图

python - 如何在 Python 中创建精确召回曲线图来比较 2 个分类器?

r - str_extract特定模式

r - 在 R 中将元素从数组的前面移动到数组的后面

R:nrow[w] * ncol[w] 中的错误:二元运算符的非数字参数,同时使用神经网络包

javascript - angularJS中的错误

python - 将字符串数据传递给 matplotlib API 时会绘制什么?