Home > Enterprise >  Assign the value (1) to NA in tiff files
Assign the value (1) to NA in tiff files

Time:08-01

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)

CodePudding user response:

How large is your raster files and how limited by memory are you? With raster, the optimal memory safe approach when interacting with large files is to use the reclassify function shown below. Let me know if it works.

# Package names
library(raster)

# Read in files
R1 <- raster("D:/Results/1.tiff")
R2 <- raster("D:/Results/1.tiff")

# use the reclassify function to group values to other values. 
# In this case, NA values to 1.Reclassification is done with matrix rcl , 
# in the row order of the reclassify table.
D1 <- reclassify(R1, cbind(NA, 1))
D2 <- reclassify(R2, cbind(NA, 1))

# Find the difference between the two and plot.
Se1 = R2-R1
plot(Se1)

CodePudding user response:

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)
  • Related