I have hundreds of rasters spanning 10 years, files are stored by year (e.g., 2020, 2019, 2018, etc.). I would like to mosaic all the raster files within a directory, move to the next directory and repeat.
Normally, I do something like this to mosaic a list of rasters within a single directory:
(file_list <- list.files(here("file/path"),
pattern="*6.tif$", full.names = TRUE, recursive=FALSE))
# raster list
(rast.list <- lapply(1:length(file_list), function(x) {raster(file_list[x])}))
# And then use do.call on the list of raster objects
rast.list$fun <- max
rast.mosaic <- do.call(mosaic,rast.list)
How can I adapt this so that all the files in a single directory are mosaiced to a single file, and then move to the next directory? I'm using raster, but am open to stars
or terra
options
paste0
to get the path to the rasters for that year.