使用缓冲区在raster :: extract中创建带有属性信息的数据框

问题描述 投票:1回答:1

我有以下情况:

library(spatstat)
library(raster)

#Create a SpatialPointsDataFrame
pts <- rpoispp(60) ## Coordinates
status<-rep(c("control","treat"),15)
d<-data.frame(pts$x[1:30],pts$y[1:30],status)
colnames(d)<-c("x","y","status")
pts.sampling = SpatialPoints(cbind(pts$x[1:30],pts$y[1:30]), proj4string=CRS("+proj=utm +zone=22 +south +datum=WGS84 +units=m +no_defs"))
df.pts.SPDF<- SpatialPointsDataFrame(pts.sampling, data = d)

#Create some rasters
r <- raster(ncol=10, nrow=10)
s <- stack(lapply(1:4, function(i) setValues(r, runif(ncell(r)))))

## Extract raster values in 6 distance around (buffer) and organize the results 
res<- data.frame(coordinates(pts.sampling),
                   df.pts.SPDF,
                   extract(s, df.pts.SPDF,buffer=6))

#Error in data.frame(coordinates(pts.sampling), df.pts.SPDF, extract(s,  : 
#  arguments imply differing number of rows: 30, 8

我想在最终数据帧中恢复属性信息(在我的情况下为df.pts.SPDF$status变量)。我没有办法向任何函数解释邻域坐标(buffer=6附近)具有与原始坐标(pts.sampling)相同的状态属性。有任何想法吗?

r raster r-raster
1个回答
0
投票

我不确定我是否知道您想做什么,因为您肯定需要提出更好的解释,但这是您想要的吗?

## Extract raster values in 6 distance around (buffer) and organize the results 
x <- extract(s, df.pts.SPDF,buffer=6)
res<- data.frame(coordinates(pts.sampling),
                 df.pts.SPDF,
                 do.call("rbind", x))

> head(res)
#  coords.x1 coords.x2         x         y  status coords.x1.1 coords.x2.1 optional   layer.1   layer.2   layer.3   layer.4
#1 0.8824756 0.1675364 0.8824756 0.1675364 control   0.8824756   0.1675364     TRUE 0.2979335 0.8745829 0.4586767 0.4631793
#2 0.3197404 0.6779792 0.3197404 0.6779792   treat   0.3197404   0.6779792     TRUE 0.2979335 0.8745829 0.4586767 0.4631793
#3 0.1542464 0.5778322 0.1542464 0.5778322 control   0.1542464   0.5778322     TRUE 0.2979335 0.8745829 0.4586767 0.4631793
#4 0.6299502 0.3118177 0.6299502 0.3118177   treat   0.6299502   0.3118177     TRUE 0.2979335 0.8745829 0.4586767 0.4631793
#5 0.4714429 0.1400559 0.4714429 0.1400559 control   0.4714429   0.1400559     TRUE 0.2979335 0.8745829 0.4586767 0.4631793
#6 0.4568768 0.6155193 0.4568768 0.6155193   treat   0.4568768   0.6155193     TRUE 0.2979335 0.8745829 0.4586767 0.4631793
© www.soinside.com 2019 - 2024. All rights reserved.