?raster illustrates that raster::raster() has a band argument: Integer. The layer to use in a multi-layer file.
Since you are reading netCDF files (with 365 layers in your case), it seems like raster::raster() is only able to read one layer at once. If you don't provide any band argument, band = 1 is chosen per default.
library(raster)
library(ncdf4)
raster::raster("chirps-v2.0.2006.days_p25.nc")
#> class : RasterLayer
#> band : 1 (of 365 bands)
#> dimensions : 400, 1440, 576000 (nrow, ncol, ncell)
#> resolution : 0.25, 0.25 (x, y)
#> extent : -180, 180, -50, 50 (xmin, xmax, ymin, ymax)
#> crs : +proj=longlat +datum=WGS84 +no_defs
#> source : chirps-v2.0.2006.days_p25.nc
#> names : Climate.Hazards.group.InfraRed.Precipitation.with.Stations
#> z-value : 2006-01-01
#> zvar : precip
raster::raster("chirps-v2.0.2006.days_p25.nc", band = 40)
#> class : RasterLayer
#> band : 40 (of 365 bands)
#> dimensions : 400, 1440, 576000 (nrow, ncol, ncell)
#> resolution : 0.25, 0.25 (x, y)
#> extent : -180, 180, -50, 50 (xmin, xmax, ymin, ymax)
#> crs : +proj=longlat +datum=WGS84 +no_defs
#> source : chirps-v2.0.2006.days_p25.nc
#> names : Climate.Hazards.group.InfraRed.Precipitation.with.Stations
#> z-value : 2006-02-09
#> zvar : precip
The object returned is a single-band RasterLayer. You can import a multi-layered file (such as data coming from a netCDF container) at once making use of raster::stack() instead:
nc_data <- raster::stack("chirps-v2.0.2006.days_p25.nc")
nc_data
#> class : RasterStack
#> dimensions : 400, 1440, 576000, 365 (nrow, ncol, ncell, nlayers)
#> resolution : 0.25, 0.25 (x, y)
#> extent : -180, 180, -50, 50 (xmin, xmax, ymin, ymax)
#> crs : +proj=longlat +datum=WGS84 +no_defs
#> names : X2006.01.01, X2006.01.02, X2006.01.03, X2006.01.04, X2006.01.05, X2006.01.06, X2006.01.07, X2006.01.08, X2006.01.09, X2006.01.10, X2006.01.11, X2006.01.12, X2006.01.13, X2006.01.14, X2006.01.15, ...
As you can see, the result consists of 365 layers this time. You can now subset the RasterStack object to query individual layers if you like, ...
nc_data[[40]]
#> class : RasterLayer
#> band : 40 (of 365 bands)
#> dimensions : 400, 1440, 576000 (nrow, ncol, ncell)
#> resolution : 0.25, 0.25 (x, y)
#> extent : -180, 180, -50, 50 (xmin, xmax, ymin, ymax)
#> crs : +proj=longlat +datum=WGS84 +no_defs
#> source : chirps-v2.0.2006.days_p25.nc
#> names : X2006.02.09
#> zvar : precip
... and you can now simply make use of raster::mean() on your stack to calculate average values over all 365 layers, corresponding to the yearly average you requested, and write the resulting single-band raster to disk using raster::writeRaster():
nc_mean <- raster::mean(nc_data)
nc_mean
#> class : RasterLayer
#> dimensions : 400, 1440, 576000 (nrow, ncol, ncell)
#> resolution : 0.25, 0.25 (x, y)
#> extent : -180, 180, -50, 50 (xmin, xmax, ymin, ymax)
#> crs : +proj=longlat +datum=WGS84 +no_defs
#> source : memory
#> names : layer
#> values : 0, 71.93983 (min, max)
If you want to find out how to read and write netCDF files using terra, see my other post. Just make sure to change the file extension in terra::writeRaster() from asc to tif.