下面是光栅库为使用clusterR和overlay函数提供的一个示例:
library(raster)
beginCluster()
r <- raster()
r[] <- 1:ncell(r)
s <- stack(r, r*2, r*3)
f2 <- function(d,e,f) (d + e) / (f * param)
param <- 122
ov <- clusterR(s, overlay, args=list(fun=f2), export='param')如果我有多个光栅堆栈,我想知道如何运行这个函数:
s <- stack(r, r*2, r*3)
s2 <- stack(r*2, r*3, r*4)
s3 <- stack(r*3, r*4, r*5)我想要这样的东西(函数d,e,f中的f2是s, s2和s3中的每个层):
ov <- clusterR(s,s2,s3, overlay, args=list(fun=f2), export='param')发布于 2017-03-01 14:19:56
首先,我将在堆栈中创建一个虚拟光栅层,保存param值。因此,这些操作可以矢量化:
p <- 122
rp <- r
rp[] <- p
s <- stack(s, rp)
s2 <- stack(s2, rp)
s3 <- stack(s3, rp)然后,您可以这样更改您的功能:
f2 <- function(x) (x[[1]] + x[[2]]) / (x[[3]] * x[[4]])因此,正确地引用了各个堆栈x的层。第四层是param值(此处为p)
然后创建一个层堆栈列表:
stackList <- list(s, s2, s3)然后是lapply clusterR函数。
ov <- lapply(stackList, function(x){clusterR(x, fun = f2, progress = "text")})然后,ov将是覆盖层的列表。
https://stackoverflow.com/questions/35369137
复制相似问题