我有一个list
的raster stacks
,每个栅格堆栈包含不等数量的栅格。如何总结列表中的栅格数量?我试过length()
,但这只返回我列表中的堆栈数量!示例数据:
library(raster)
#reproducible example
set.seed(987)
#our list of rasters
r.lst <- as.list(1:3)
# setting up list pf raster stacks
r1 <- raster(nrows = 1, ncols = 1, res = 0.5, xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5, vals = runif(36, 1, 5))
r.lst[[1]] <- stack(lapply(1:7, function(i) setValues(r1,runif(ncell(r1)))))
r.lst[[2]] <- stack(lapply(1:3, function(i) setValues(r1,runif(ncell(r1)))))
r.lst[[3]] <- stack(lapply(1:2, function(i) setValues(r1,runif(ncell(r1)))))
一种方法如下:
# count sum rasters
n <- Reduce(`+`, lapply(r.lst, nlayers))
n
#[1] 12
您可以使用nlayers
获取堆栈中的层数(栅格),并使用lapply
将该函数应用于列表的每个元素:
lapply(my.list.of.rasterstacks, nlayers)
总结一下:
sum(unlist(lapply(my.list.of.rasterstacks, nlayers)))
怎么样:
sum(sapply(r.lst, nlayers))
# [1] 12
要么
sum(sapply(r.lst, dim)[3, ])
# [1] 12
没有最少的数据,很难回答
编辑
通过示例数据,@ Where我的毛巾有正确的答案
sum(unlist(lapply(r.lst, nlayers)))
考虑一下this post如何为将来的问题制作一个好的可重现的例子。