簡體   English   中英

錯誤的原因是什么:R中二進制運算符的非數字參數

[英]What is the reason of the error: non-numeric argument to binary operator in R

我試圖根據異常數據(+/-)用單獨的列進行分組(按年)條形圖。 我在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"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM