r语言 - 循环矩阵"arguments imply differing number of rows"错误



我有一个数据集,包括三种土地覆盖类型的月ndvi和降水数据,共26个站点,每个站点13年。我想运行一个循环,在矩阵m1中填写"ndvi"one_answers"cv",用于每年每个站点每个土地覆盖类型。最后,我需要三个环形土地覆盖矩阵中的三个图。

当我输入如下所示的脚本时,我遇到了一个错误。我不确定为什么会有"不同的行数",因为每个站点每年都有一个最大ndvi值和一个cv值。有没有人能给我提个建议,告诉我哪里出了问题?这个脚本工作为我的分析与平均ndvi,但不知何故与max它不。

dput(head(d))
structure(list(row.names = c(1L, 1769L, 2055L, 2341L, 2627L, 
2913L), timestamp = 1:6, station = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("Aiselukharka", "Anarmani", "BiratnagarAirport", 
"Chainpur", "Chandragadhi", "Damak", "Dhankuta", "Diktel", "Dingla", 
"Haraicha", "Ilam", "Kanyam", "Kechana", "KhotangBazar", "Leguwa", 
"Letang", "ManebhanjyangBazar", "Muga", "Mulghat", "Num", "Okhaldunga", 
"PakhribasBazar", "Phidim", "Sanischare", "Sankhuwasabha", "Tumlingtar"
), class = "factor"), year = c(2000L, 2000L, 2000L, 2000L, 2000L, 
2000L), month = structure(c(5L, 4L, 8L, 1L, 9L, 7L), .Label = c("apr", 
"aug", "dec", "feb", "jan", "jul", "jun", "mar", "may", "nov", 
"oct", "sept"), class = "factor"), ndvi = c(0.4138, 0.4396, 0.4393, 
0.6029, 0.4756, 0.4969), landcover = structure(c(3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("Cropland/Natural vegetation mosaic", 
"Croplands", "Mixed forest"), class = "factor"), altitude = c(2143L, 
2143L, 2143L, 2143L, 2143L, 2143L), altrange = structure(c(3L, 
3L, 3L, 3L, 3L, 3L), .Label = c("0-500", "1501-2000", "2001+", 
"501-1500"), class = "factor"), precipitation = c(16, 4, 25.5, 
72.6, 241.7, 505.9)), .Names = c("row.names", "timestamp", "station", 
"year", "month", "ndvi", "landcover", "altitude", "altrange", 
"precipitation"), row.names = c(NA, 6L), class = "data.frame")
d <- read.csv("asort.csv", header = TRUE, sep = ",")
stations <- levels(d$station)
landcover <- levels(d$landcover)
allyears=c$year[ ! duplicated( c$year)]
for(lc in landcover) {
m1=NULL
for(j in stations){
  for (i in allyears){
      tmp <- d[d$landcover==lc & d$station==j & d$year==i,]
      ndvi<- tmp$ndvi[which.max(tmp$ndvi)];  
      precip_2m<-tmp$precipitation[tmp$month %in% c("feb","mar","apr","may","jun","jul","aug")]
      cv<-sd(precip_2m,na.rm=T)/mean(precip_2m, na.rm=T)
      station=j
      landcover=lc
      year=i
      lag=l
      m1 = rbind(m1, data.frame(ndvi, cv,landcover, station, year))
  }
}
 cat("landcover=",lc) 
 print(summary(aov(ndvi~cv,data=m1)))
 plot(ndvi~cv,main=lc,
     xlab="cv of growing season precipitation", ylab="max ndvi ", data=m1)
 abline(lm(ndvi~cv, data=m1))
 fit = summary(lm(ndvi~cv, data=m1))
 r2 = fit$adj.r.squared  
 my.p = fit$coefficients[2,4]
 rp = vector('expression',2)
 rp[1] = substitute(expression(italic(R)^2 == value.r), list(value.r = format(r2,dig=3)))[2]                  
 rp[2] = substitute(expression(italic(p) == value.p), list(value.p = format(my.p, digits = 2))[2]                  
legend('topright', legend = rp, bty = 'n')
}

Error in data.frame(ndvi, cv, landcover, station, year) : 
arguments imply differing number of rows: 0, 1 

谢谢!

当特定子集(nrow(tmp)==0)中没有值时,我得到该错误。mean和你现在所做的之间的区别是mean(NULL)实际上返回了一个长度为1的向量,而tmp$ndvi[which.max(tmp$ndvi)]将返回一个长度为0的向量。这是您指定的值(如站点,土地覆盖等)之间的不匹配,长度总是为1,而您正在计算的值可能为零长度,因此存在不匹配错误。

你可以做两件事。最简单的方法是替换

ndvi<- tmp$ndvi[which.max(tmp$ndvi)];  

ndvi<- max(tmp$ndvi);  

因为maxmean的行为相同,因为它会返回一些东西。当然,这意味着你最终得到的数据很奇怪。另外,您可以测试空tmp数据。饥饿与

for (i in allyears){
    tmp <- d[d$landcover==lc & d$station==j & d$year==i,]
    if(nrow(tmp)>0) {
       ...
       m1 = rbind(m1, data.frame(ndvi, cv,landcover, station, year))
    }
}

但实际上似乎您应该能够使用aggregate来计算大多数这些值(尽管您可能需要多次调用不同的摘要函数)。

相关内容

最新更新