错误:nrow(ref)和nrow(target)必须是> 0?

时间:2017-03-08 19:45:41

标签: r raster sp

基于Unable to writeRaster for signature "rasterPCA", "character",我获得了两个光栅,它们是PC1和PC2中的一系列气候变量。但是,无论具有相同的范围和分辨率,当加载到R中时,我的全局环境中的单元格数量会有所不同。

以下是我正在使用的代码,该代码来自Hamann等人的附录,2015年,我收到此错误:

library(SDMTools)     # install package to read and write ESRI ASCII grids
library(yaImpute)     # install package for k-nearest neighbour (kNN) search

lg1 <- asc2dataframe("C:\\Users\\rameshv\\LGM\\4_PCAforR\\PC_1.asc") # principal component grids
lg2 <- asc2dataframe("C:\\Users\\rameshv\\LGM\\4_PCAforR\\PC_2.asc")
present1  <-asc2dataframe("C:\\Users\\rameshv\\Present\\4_PCAforR\\PC_1.asc")
present2  <- asc2dataframe("C:\\Users\\rameshv\\Present\\4_PCAforR\\PC_2.asc")

idxy <- cbind(id=1:nrow(lg1),lg1[,1:2])   # data frame of IDs and XY coords
b <- (max(lg1$var.1)-min(lg1$var.1))/120  # bin size for 120 PC1 bins

l1 <- round(lg1$var.1/b)              # convert PC1 to 120 bins via rounding
l2 <- round(lg2$var.1/b)              # convert PC2 to <120 bins via rounding
p1 <- round(present1$var.1/b)               # same for present PC1
p2 <- round(present2$var.1/b)               # same for present PC2
l  <- paste(l1,l2)                         # PC1/PC2 combinations in LGM climate
p  <- paste(p1,p2)                         # PC1/PC2 combinations in present climate
u  <- unique(p)[order(unique(p))]          # list of unique PC1/PC2 combinations

sid <- c()                                 # empty vector for source IDs
tid <- c()                                 # empty vector for target IDs
d   <- c()                                 # empty vector for distances

for(i in u){                          # loop for each unique PC1/PC2 combination
lxy <- idxy[which(l==i),]           # coordinates of i-th combination in LGM
pxy <- idxy[which(p==i),]           # coordinates of i-th combination in present
sid <- c(sid, lxy$id)               # append i-th PC1/PC2 combination to previous 

if(nrow(pxy)>0){                    # kNN search unless no-analogue climate
  knn <- data.frame(ann(as.matrix(pxy[,-1]), as.matrix(lxy[,-1]), k=1)$knnIndexDist)      
  tid <- c(tid, pxy[knn[,1],"id"]) # the IDs of the closest matches  
  d <- c(d, sqrt(knn[,2]))         # their corresponding geographic distances
}
 else {                              # else statement for no-analogue climates
 tid <- c(tid, rep(NA,nrow(lxy))) # flag destinations as missing for no analogues
 d <- c(d, rep(Inf,nrow(lxy)))    # flag distances as infinity for no analogues
 }
}

在for循环结束时,我收到以下错误:

Error in ann(as.matrix(pxy[, -1]), as.matrix(lxy[, -1]), k = 1) : 
error: nrow(ref) and nrow(target) must be > 0

我不确定这个错误是否与细胞数量的差异有关?有什么建议吗?

修改

根据Bastien的评论,我调查了结构,我得到了这个:

> str(as.matrix(pxy[,-1]))
  num [1:27, 1:2] 8.1 8.14 8.22 8.97 9.01 ...
  - attr(*, "dimnames")=List of 2
  ..$ : chr [1:27] "1" "8" "33" "583" ...
  ..$ : chr [1:2] "y" "x"

> str(as.matrix(lxy[,-1]))
  logi[0 , 1:2] 
  - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "y" "x"

连连呢?

1 个答案:

答案 0 :(得分:0)

LoBu是正确的,你的lxy数组是空的。目前尚不清楚这出错的地方 - 您的PCA可能已失败,您的箱号大小计算b可能已失败。如果您正在尝试匹配像Hamann等人那样的分类气候值,那么您的pxy数据框也太短了。 2015.如果没有近距离看到您的数据,就无法说明如何解决这个问题 - 我建议您检查您的PCA输出和基础气候栅格。

你的学习区有多大?附录3是迄今为止最复杂的气候模拟方法。我建议尝试附录2,如果你不能以高分辨率工作,它会慢一点但效果很好。这是代码,供快速参考。

library(SDMTools)       # install package to read and write ESRI ASCII grids
present <- asc2dataframe("C:\Your Path\MAT6190.asc")
future  <- asc2dataframe("C:\Your Path\MAT2020s.asc")

t <- 0.25               # plus/minus threshold to define climate match
t <- 1/(t*2)            # inverse for rounding, double for plus/minus

x <- present$x                    # vector of grid cell x coordinates
y <- present$y                    # vector of grid cell y coordinates
p <- round(present$var.1*t)/t     # vector of rounded present climate values 
f <- round(future$var.1*t)/t      # vector of rounded future climate values 
d <- vector(length=length(p))     # empty vector to write distance to climate match

u     <- unique(p)[order(unique(p))]    # list of unique climate values in p
match <- function(u){c(which(u==f))}    # function finding climate matches of u with f
m     <- sapply(u, match)               # list of climate matches for unique values

for(i in 1:length(p)){                  # loop for all grid cells of p
   mi   <- m[[which(u==p[i])]]          # recalls list of climate matches for p[i]
   d[i] <- sqrt(min((x[i]-x[mi])^2 + (y[i]-y[mi])^2))    # distance to closest match
   }

# writes out log10 speed and distance multiplied by 100 in ESRI ASCII format
# conversion: -200=0.01km, -100=0.1km, 0=1km, 100=10km, 200=100km etc.
d[d==Inf] <- 100000  # sets no analogue to 10,000km
out=cbind(y,x,logDist=round(log10(d)*100),logSpeed=round(log10(d/50)*100))
dataframe2asc(out)