0

I use the raster package to read a NetCDF file and want to process it in R. A couple of manipulations give me the RasterBrick object r_daily with 1461 layers that correspond to daily values over 4 years:

> r_daily
class      : RasterBrick 
dimensions : 360, 720, 259200, 1461  (nrow, ncol, ncell, nlayers)
resolution : 0.5, 0.5  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
> names(r_daily)
[1] "y2011.01.01" "y2011.01.02" "y2011.01.03" "y2011.01.04"
[5] "y2011.01.05" "y2011.01.06" "y2011.01.07" ...

Now I just want to sum over all values of each year to get the RasterBrick object r_yearly.

> r_yearly
class      : RasterBrick 
dimensions : 360, 720, 259200, 4  (nrow, ncol, ncell, nlayers)
resolution : 0.5, 0.5  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 

I am grateful for any help or hints!

McCuys
  • 37
  • 5

1 Answers1

1

I now found a good solution in this post that I did not encounter before.

    years <- names(r_daily) %>%
      substr(2,5) %>%
      as.numeric()
    r_yearly<- raster::stackApply(r_daily, years, fun = sum)
McCuys
  • 37
  • 5