I want to loop through two directories simultaneously. Both directories contain raster-images, one the initial images the other one the masks for these images.
I would like to access them outside the loops since I have to do some calculations but also keep my folder structures.
The loops don't have to be nested in each other, I guess, but how can I access the files outside the loop? Creating an empty stack/raster outside and fill it with the values from the stack/raster of the loops does not seem to the trick.
So far I got this:
library(raster)
files1 = list.files(path2, pattern = "*.tif", full.names = TRUE)
files2 = list.files(path4, pattern = "*_cmask.tif", full.names = TRUE)
f1Stack = stack()
for (f1 in files1) {
f1Stack = stack(f1)
do stuff with f1Stack
for (f2 in files2) {
f2Raster = raster(f2)
do stuff with f2Raster
}
}
edit:
I want to store the Raster Stack created inside the loop to be accessible outside. files1 contains 10 multiband-tiffs. If I create an empty stack outside the loop try to update it with the current stack in the loop, it always contains just the last stack of the loop.