I have about 468 layers in my raster stack (new layer every 16 days between 2000 and 2020). Is there a way to group the layers by year and instead have just 21 layers?

[1] "X2000.02.18" "X2000.03.05" "X2000.03.21" "X2000.04.06" "X2000.04.22"
[6] "X2000.05.08" "X2000.05.24" "X2000.06.09" "X2000.06.25" "X2000.07.11"

1 Answer 1


Check out the rts (Raster Time Series) package, which should be able to do this for you. Assuming your stack has been read in as a raster stack or brick using the raster package and layers are named after their dates in the format posted:

YourRaster_ts <- rts(YourRaster, as.Date(names(YourRaster), format = "X%Y.%m.%d"))

Then you can use apply.period to summarise your layers however you want (mean, sum, min, max values etc.). So for yearly means:

YourRaster_yearlyMean <- apply.yearly(YourRaster_ts, mean) #add na.rm argument if required

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.