我有一些栅格,我想计算移动平均线,然后再次重写栅格。
r1 <- r2 <- r3 <- r4 <- r5 <- r6 <- raster(nrows=10, ncols=10);
r1 <- setValues(r1,runif(100,min=1,max=100));
r2 <- setValues(r2,runif(100,min=1,max=100));
r3 <- setValues(r3,runif(100,min=1,max=100));
r4 <- setValues(r4,runif(100,min=1,max=100));
r5 <- setValues(r5,runif(100,min=1,max=100));
r6 <- setValues(r6,runif(100,min=1,max=100));
# Stack them
st1 <- stack(r1,r2,r3,r4,r5,r6)
#compute the moving average:
x <- calc(st1, function(x) movingFun(x, 3, mean))
现在x
中的图层应重新计算为:
firstlayer=(first layer + fourth layer)/2
secondlayer=(second layer + fifth layer)/2
thirdlayer=(third layer + sixth layer)/2
这些是示例,但我的数据不止于此,所以无论我在x
答案 0 :(得分:2)
您可以这样做:
library(raster)
# creating the same data set, but in fewer lines
r <- raster(nrows=10, ncols=10)
s <- stack(lapply(1:6, function(...) setValues(r, runif(100,min=1,max=100))))
x <- calc(st1, function(x) movingFun(x, 3, mean))
# remove the edges to line up the layers that need to be added
nl <- nlayers(x)
x1 <- x[[-c((nl-2):nl)]]
x2 <- x[[-c(1:3)]]
z <- x1 + x2 / 2
请参阅?stackApply了解其他一些案例