0

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?

amrit
  • 1

1 Answers1

5

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.
}
XavierCLL
  • 1,163
  • 10
  • 12
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63