raster::extract:创建数据帧并使用缓冲区连接属性信息,但包括NAs问题
在两个光栅中使用extract()函数后,我希望在最后一个数据帧raster::extract:创建数据帧并使用缓冲区连接属性信息,但包括NAs问题,r,raster,r-raster,R,Raster,R Raster,在两个光栅中使用extract()函数后,我希望在最后一个数据帧RES中恢复SpatialPointsDataFrame(df.pts.SPDF$status变量)的属性信息。我没有办法在任何函数中解释邻域坐标(buffer=6左右)与原始坐标具有相同的状态属性(df.pts.SPDF),而且我对包含的NAs也有太多问题。对于我使用的NAs,x我认为您期望的输出可能会有所不同。上面的x和y坐标不属于您的数据。但我提供了两种解决方案: 解决方案1:与所需输出类似: #这是一个将向量转换为矩阵的函数
RES
中恢复SpatialPointsDataFrame(df.pts.SPDF$status
变量)的属性信息。我没有办法在任何函数中解释邻域坐标(buffer=6左右)与原始坐标具有相同的状态属性(df.pts.SPDF
),而且我对包含的NAs也有太多问题。对于我使用的NAs,x我认为您期望的输出可能会有所不同。上面的x和y坐标不属于您的数据。但我提供了两种解决方案:
解决方案1:与所需输出类似:
#这是一个将向量转换为矩阵的函数
c2m
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
#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.")
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.")