Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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).

share|improve this question

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.