【发布时间】:2019-11-24 16:13:12
【问题描述】:
在两个栅格中使用 extract() 函数后,我想在我的最终数据框 RES 中恢复我的 SpatialPointsDataFrame(在我的情况下为 df.pts.SPDF$status 变量)的属性信息。我找不到在任何函数中解释邻域坐标(缓冲区 = 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
有什么想法吗?
【问题讨论】: