raster :: extract:使用缓冲区创建数据框并加入属性信息,但包含NA的问题

时间:2019-11-23 18:42:22

标签: r raster r-raster

在两个栅格中使用extract()函数之后,我想在最终数据帧df.pts.SPDF$status中恢复SpatialPointsDataFrame(在我的情况下为RES变量)的属性信息。在任何函数中,我都找不到一种方法来解释邻域坐标(buffer = 6附近)具有与原始坐标(df.pts.SPDF)相同的状态属性,并且我对包含的NA也有问题。对于NA,我使用x<-lapply(list, function(x) x[!is.na(x)])失败了。 在我的示例中:

library(raster)  
r <- raster(ncol=10, nrow=10, crs="+proj=utm +zone=1 +datum=WGS84", xmn=0, xmx=50, ymn=0, ymx=50)
s1 <- stack(lapply(1:4, function(i) setValues(r, runif(ncell(r)))))
r2 <- raster(ncol=10, nrow=10, crs="+proj=utm +zone=1 +datum=WGS84", xmn=0, xmx=100, ymn=0, ymx=100) # Large raster for produce NAs
s2 <- stack(lapply(1:4, function(i) setValues(r2, runif(ncell(2)))))
ras <- list(s1, s2)
pts <- data.frame(pts=sampleRandom(s2, 100, xy=TRUE)[,1:2], status=rep(c("control","treat"),5))
pts.sampling = SpatialPoints(cbind(pts$pts.x,pts$pts.y), proj4string=CRS("+proj=utm +zone=1 +datum=WGS84"))
df.pts.SPDF<- SpatialPointsDataFrame(pts.sampling, data = pts)

## Extract raster values in 6 distance around (buffer) and organize the results with df.pts.SPDF$status information 
#( neighborhood coordinates (buffer=6 around) has the same status attribute of the original coordinates in df.pts.SPDF) 
RES <- NULL
for (i in 1:length(ras)) {
x <- extract(ras[[i]], df.pts.SPDF,buffer=6)
res<- data.frame(coordinates(pts.sampling),
                 df.pts.SPDF,
                 do.call("rbind", x))
RES<-rbind(RES,c(res))                 
}
#
Error in data.frame(coordinates(pts.sampling), df.pts.SPDF, do.call("rbind",  : 
  arguments imply differing number of rows: 100, 165

我想要的输出是:

#  coords.x1 coords.x2         x         y  ras    status layer.1   layer.2   layer.3   layer.4
#1 0.8824756 0.1675364 0.8824756 0.1675364   s1    control 0.2979335 0.8745829 0.4586767 0.4631793
#2 0.3197404 0.6779792 0.3197404 0.6779792   s1    treat   0.2979335 0.8745829 0.4586767 0.4631793
#3 0.1542464 0.5778322 0.1542464 0.5778322   s1    control 0.2979335 0.8745829 0.4586767 0.4631793
#4 0.6299502 0.3118177 0.6299502 0.3118177   s1    control 0.2979335 0.8745829 0.4586767 0.4631793
#5 0.4714429 0.1400559 0.4714429 0.1400559   s1    control 0.2979335 0.8745829 0.4586767 0.4631793
#6 0.4568768 0.6155193 0.4568768 0.6155193   s1    treat   0.2979335 0.8745829 0.4586767 0.4631793

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我认为您想要的输出可能会有所不同。上面的x和y坐标不属于您的数据。但是我提供了两种解决方案:

解决方案1:与您期望的输出类似:

 #this is a function to convert vectors to matrix
c2m <- function(x){
  mtx <- matrix(x, nrow=length(x)/4, ncol=4, byrow = T)#4 is number of layers in raster stack
  return(mtx)
}

RES <- list() #you might need a list here
for (i in 1:length(ras)) {
  x <- raster::extract(ras[[i]], df.pts.SPDF, buffer=6)

  max.len <- max(sapply(x, length))
  x <- lapply(x, function(x) {c(x, rep(NA, max.len - length(x)))})
  xx <- lapply(x, function(x) c2m(x))
  res<- data.frame(coordinates(pts.sampling),
                   df.pts.SPDF,
                   do.call("rbind", xx))
  RES[[i]]<-res  #this is another change you need    
}

df.out <- ldply(RES, rbind)
colnames(df.out) <- stringr::str_replace_all(colnames(df.out), pattern = "X", replacement = "layer.")

由于有一个缓冲区,因此每个x和y都有4个点,因此某些行的坐标是重复的。这意味着如果以后将其转换为shapefile(x和y每100次观察重复一次),您将拥有叠加点。

解决方案2:将属于唯一x和y的所有值放在一行中:

RES <- list() #you might need a list here
for (i in 1:length(ras)) {
  x <- raster::extract(ras[[i]], df.pts.SPDF, buffer=6)

  max.len <- max(sapply(x, length))
  x <- lapply(x, function(x) {c(x, rep(NA, max.len - length(x)))})

  res<- data.frame(coordinates(pts.sampling),
                   df.pts.SPDF,
                   do.call("rbind", x))
  RES[[i]]<-res  #this is anotherchange    
}

df.out <- ldply(RES, rbind)
colnames(df.out) <- stringr::str_replace_all(colnames(df.out), pattern = "V", replacement = "layer.")