Search code examples
rrastertiff

Assign the value (1) to NA in tiff files


I have two raster files (values ranges from 0 to 1) and I want to find the difference between them. But the problem is there are certain values those are missing. So I want to assign them value 1 (Like NA=1). How can I do this? Any expert can solve this little query. Thanks

My code is this.

  library(raster)
  R1 <- raster ("D:/Results/1.tiff")
  R2 <- raster ("D:/Results/2.tiff")

 Se1= R2-R1
 plot(Se1)

Solution

  • Here is how you may do that with "terra" (the replacement of "raster")

     library(terra)
     R <- rast(paste0("D:/Results/", 1:2, ".tiff"))
     R <- subst(R, NA, 1)
     Se1 <- diff(R)