I have a set of rasters that I would like to subtract from another set of rasters, using simple raster math, but am having trouble matching the right rasters with their partners.
library(terra)
Simple raster math can be conducted easily between two rasters:
s <- rast(ncols = 22, nrows = 25, nlyrs = 1, xmin = 0, vals = 10)
p <- rast(ncols = 22, nrows = 25, nlyrs = 1, xmin = 0, vals = 8)
r <- s - p
However, I have a series of many rasters in a folder, and would like to conduct this math across many rasters, pairing them with their correct partners by matching strings in their files names.
My files look similar to this:
above_a.tif
above_b.tif
above_c.tif
below_a.tif
below_b.tif
below_c.tif
I would like to subtract all "below" rasters from all "above" rasters, based on their matching string at the end of their name. For example, I would like to subtract below_b.tif from above_b.tif .
I have tried the following to do this, but these code don't do what they need to: First, make a list of the two file types, which works well:
list_above <- list.files(pattern = 'above', all.files = T, full.names = F)
list_below <- list.files(pattern = 'below', all.files = T, full.names = F)
Then, I tried subtracting them, but could not figure out how to incorporate the fact that I want to pair them by matching file name string:
temp_sub <- lapply(list_above, function(x) {
to_subtract <- list_below
subtracted <- x - to_subtract
subtracted
})
Has someone done this before? It seems like something that would happen often with large spatial datasets. Maybe are you putting it into some kind of organized raster stack?
With
f_above <- list.files(pattern = 'above', all.files = T, full.names = F)
f_below <- list.files(pattern = 'below', all.files = T, full.names = F)
And assuming that the rasters all have the same extent and resolution, you can do
library(terra)
above <- rast(f_above)
below <- rast(f_below)
x <- above - below
if the rasters are not aligned, you could do
y <- lapply(1:length(f_above), \(i) {
rast(f_above[i]) - rast(f_below[i])
})