I have multiple NetCDF files that contain crop and water data. These have around 30 levels of output (for 50 years). I would like to import these and sum them afterwards in R.
Currently, I am importing the levels as follows:
library(raster)
Crop1 <- Brick("Netcdfile", varname = yield, level)
Crop2 <- Brick("Netcdfile", varname = yield, level = 2)
CropN <- Brick("Netcdfile", varname = yield, level = N)
Crop1_mean <- mean(crop1[[1:50])
Crop2_mean <- mean(crop2[[1:50])
CropN_mean <- mean(cropN[[1:50])
Crop_Sum <- mosaic (Crop1_mean, Crop2_mean, CropN_mean, fun = sum)
This is quite a lot of code to write for 30 levels when you have to do it for various projections/variables. Is there a way to write a loop for the importing of the 30 levels and sum the rasters afterwards?