I am fairly new to R and raster data. I have two large raster layers. One raster layer has only "1" as its values - all other values (those that were NOT 1) were set to be "NA." I would like to make the second raster layer have "NA" in the same places/pixels where the first raster has "NA." Both the raster layers have the same extent and resolution.
So far I have tried stacking the rasters and using the NAvalue function to make the raster "NA" where the other (first raster) is == "NA." This did not work.
I also thought maybe a cover() function would work, but this just changes NA values in one layer to non-NA values seen in other layers. (But I want to do the opposite- change non-NA/valid values to NA).
I would assume this is a simple fix; however, I have looked everywhere on how to perform this task and cannot find anything.
You are looking for the mask
method
library(raster)
r <- raster(ncol=10, nrow=10)
m <- raster(ncol=10, nrow=10)
values(r) <- 1:ncell(r)
values(m) <- rep(c(1,NA,1,NA), 25)
mr1 <- mask(r, m)
Because all the values or m
are 1, you could also do
mr2 <- r * m
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.