How to loop through raster brick?

走远了吗. 提交于 2019-12-25 04:42:20

问题


I have a raster brick of SCA(nrow=108,ncol=132,nlayers=365) which contains fractional snow cover. I want to make 46 stacks each of 8 layers from this and calculate maximum fractional snow cover from these 46 stacks.How can i do this?


回答1:


I think you may want to do that this way:

library(raster)
# example data
sca <- brick(nrow=108,ncol=132,nl=365) 
values(sca) <- runif(ncell(sca)*nlayers(sca))

# indices grouping sets of 8
i <- rep(1:ceiling(365/8), each=8)
# the last period is not a complete set of 8 days
i <- i[1:nlayers(sca)]

x <- stackApply(sca, i, max)

If you wanted a loop (but this is R, try to avoid loops) you could do

for (i in 1:nlayers(sca)) {
   x <- sca[[i]]
   # etc.
}


来源:https://stackoverflow.com/questions/36374990/how-to-loop-through-raster-brick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!