Sum nlayers of a rasterStack in R
You can use stackApply
to do this. Using your example data, it looks like the name of each raster layer is the date. You can use that to build the indices that you need to pass to stackApply
.
The list of indices needs to have 31 1s for the January days etc.
You could do:
#get the date from the names of the layers and extract the month
indices <- format(as.Date(names(data), format = "X%Y.%m.%d"), format = "%m")
indices <- as.numeric(indices)
#sum the layers
datasum<- stackApply(data, indices, fun = sum)
The result will be a raster stack of 12 layers.
To subset raster layers from the stack, you can do data[[c(1,2]]