我有两份文件:
我想通过模拟,提取每个居住地的平均空气质量指数。为了计算平均空气质量指数,我使用一个小缓冲器(60米)。
我正在设法提取每个居住地的平均空气质量指数。但我想要一个平均每个居住地,通过模拟)。
接下来,我将尝试给出一个简单的例子:
# create 2 raster maps (as example of 2 air quality index simulations)
map_r1 = raster(ncol = 10, nrow = 10, xmn = 0, xmx = 100, ymn = 0, ymx = 100)
values(map_r1) = seq(1:ncell(map_r1))
map_r2 = raster(ncol = 10, nrow = 10, xmn = 0, xmx = 100, ymn = 0, ymx = 100)
values(map_r2) = 1:ncell(map_r2)*2
# create RasterStack adding both raster maps
map_stack<-stack(map_r1,map_r2)
# create SpatialPoint (as example of 3 places of residence)
x <- c(20,40,60)
y <- c(20,40,60)
v1 <- c(1.0, 2.0, 3.0)
map_p<-as.data.frame(cbind(x,y,v1))
coordinates(map_p) <- ~x + y然后,为了提取每个居住地的平均值,通过模拟,我尝试了以下方法:
# extract mean value (buffer=15 as example) for each point
buff<-extract(map_stack,map_p,buffer=15)
mean<-sapply(buff,mean)结果只给出每个居住地的平均值(计算所有模拟的平均值)。我很乐意听取关于如何通过模拟提取每个住宅的平均空气质量指数的想法。你好,曼纽尔
发布于 2015-04-24 16:56:25
下面是一个小示例,它向您展示了这是如何工作的(摘自提取)。
r <- raster(ncol=36, nrow=18)
r[] <- 1:ncell(r)
s <- stack(r, r*2, r^2)
xy <- cbind(-50, seq(-80, 80, by=20))
e <- extract(s, xy[2:3,], buffer=1000000)
e
#[[1]]
# layer.1 layer.2 layer.3
#[1,] 517 1034 267289
#[2,] 518 1036 268324
#[3,] 552 1104 304704
#[4,] 553 1106 305809
#[5,] 554 1108 306916
#[6,] 555 1110 308025
#
#[[2]]
# layer.1 layer.2 layer.3
#[1,] 445 890 198025
#[2,] 446 892 198916
#[3,] 481 962 231361
#[4,] 482 964 232324正如您所看到的,对于每个点,您得到一个矩阵,其中包含每个层的值。现在可以使用计算所需的内容。如果你想要一层一层的平均值。你可以
sapply(e, colMeans)
[,1] [,2]
#layer.1 541.5 463.5
#layer.2 1083.0 927.0
#layer.3 293511.2 215156.5https://stackoverflow.com/questions/29850449
复制相似问题