2014-09-20 48 views
0

我試圖根據異常數據(+/-)對單獨列進行分組(根據年份)的條形圖。 我用下面的腳本和數據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 

什麼是這種錯誤的原因是什麼?我發現了幾個問題,這個網站的二元運算符有錯誤非數字參數,但每個案例都不相同。我認爲這可能是負面的( - )值。如何避免這個錯誤?

+1

而不是告訴你如何從讀作mydata'本地文件,給我們輸出'dput(mydata)'的數據。 – 2014-09-20 03:42:38

回答

3

它不需要做負值。您將數字和字符類型混合在矩陣中,將所有內容都轉換爲字符。觀察

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) 

得到

enter image description here

這是假設你的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")) 
+0

謝謝,這正是我想要的。 – Vandka 2014-09-20 07:50:56

相關問題