I have the following situation:
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
I've like to recover attributes information ( df.pts.SPDF$status
variable in my case) in my final data frame. I don't find a way to explain to any function that the neighborhood coordinates ( buffer=6
around) has the same status attribute of the original coordinates ( pts.sampling
). Any ideas?
I am not sure if I got what you want to do as definitely you need to come up with a better explanation but Is this what you want?
## 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.