1

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

1
  • 2
    You can wrap your code in an outer loop that goes over years, and construct the path using maybe paste0 to get the path to the rasters for that year.
    – Spacedman
    Commented Aug 9, 2022 at 19:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.