I am working with daily observation of climate data organized in .nc files.
I read them using the stack command of the raster package. Each file (corresponding to a year) is a RasterStack element with the following characteristics:
class : RasterStack
dimensions : 360, 720, 259200, 365 (nrow, ncol, ncell, nlayers)
resolution : 0.5, 0.5 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
datasum<- stackApply(data, ??? ,fun=sum)
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
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