[GIS] How to replace values in rasters in directory 1 based on condition on other rasters in another directory

rraster

Having 10 raster files in one directory and another 10 rasters in another directory and both have the same dimensions.

directory 1:

    files1 <- list.files("C:\\2010", "*.envi", full.names = TRUE)
    s <- stack(files1)

directory 2:

 files2 <- list.files("C:\\2012", "*.envi", full.names = TRUE)
 s1 <- stack(files2)

I would like to filter s based on s1.
I want to replace values in all layers in s by NA whenever corresponding layers in s1 have values greater than 0.6(corresponding pixels).

Best Answer

Something like:

s1[s2 > 0.6] <- NA

This is the standard R way of doing things like this. You use s2 to make a subset in s1, and then you replace that subset by NA. You might need to do some looping if rasterstacks do not support this directly.