【问题标题】:Create a data frame with atributes information in raster::extract using buffer使用缓冲区在 raster::extract 中创建具有属性信息的数据框
【发布时间】:2019-11-23 10:28:23
【问题描述】:

我有以下情况:

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】:

    我不确定我是否得到了您想要做的事情,因为您肯定需要提出更好的解释,但这就是您想要的吗?

    ## 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2013-11-06
      • 2017-07-18
      相关资源
      最近更新 更多