I have monthly rasters for multiple years from which I would like to compute seasonal sum based on specific months (October to February).
library (terra)
#create rasters
r1 <- rast(nrows=50, ncols=50)
rr <- lapply(1:36, function(i) setValues(r1, runif(ncell(r1), min = 0, max = 1000)))
#create raster stack
stk <-rast(rr)
#create date vector
dte <- seq(as.Date("2015-1-1"), as.Date("2017-12-31"), by="month")
#apply date vector to raster names
names(stk) <- dte
Using the months assigned in the raster names, I would like to create a new raster stack by computing season sum for five months every year starting from October to February.
There are examples of extracting specific month from the stack Subsetting a Rasterstack by month or computing average for trisemester with seasons going across the years R How to calculate the seasonal average value for each year using stackApply?. In this case, the indices are of equal length.
**indices <-**
seasonal_sum <- stackApply(stk, indices, sum)
Wondering how to get indices that are unequal across years before applying stackapply (five months - October to February and seven months - March to September). Appreciate your help if there better ways to do this