简体   繁体   English

如何将栅格值设置为“NA”,其中另一个栅格具有 NA

[英]How to set raster values to “NA” where another raster has NA

I am fairly new to R and raster data.我对 R 和栅格数据相当陌生。 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."一个栅格图层的值只有“1” - 所有其他值(非 1 的值)都设置为“NA”。 I would like to make the second raster layer have "NA" in the same places/pixels where the first raster has "NA."我想让第二个栅格图层在第一个栅格具有“NA”的相同位置/像素中具有“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."到目前为止,我已经尝试堆叠栅格并使用NAvalue函数使栅格“NA”,而另一个(第一个栅格)是 ==“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.我还认为cover()函数可能会起作用,但这只是将一层中的NA 值更改为其他层中看到的非NA 值。 (But I want to do the opposite- change non-NA/valid values to NA). (但我想做相反的事情 - 将非 NA/有效值更改为 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您正在寻找mask方法

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因为所有的值或m都是 1,你也可以这样做

mr2 <- r * m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM