如何通过条件将所有值分配给NA?

发布于 2025-02-13 15:39:21 字数 554 浏览 1 评论 0原文

library(raster)
r1 <- r2 <- r3 <- raster(ncol=10, nrow=10)
r1[] <- runif(ncell(r1))
r2[] <- runif(ncell(r2)) / 2
r3[] <- runif(ncell(r3)) * 1.5
s <- stack(r1, r2, r3)


 r11 <- r22 <- r33 <- raster(ncol=10, nrow=10)
 r11[] <- runif(ncell(r11))
 r22[] <- runif(ncell(r22)) / 2
 r33[] <- runif(ncell(r33)) * 1.5
 g <- stack(r11, r22, r33)

如果g中的任何像素是&gt; = 5,则可以在s = na中进行相应的像素,

可以这样完成:

  s[g >= 5]= NA

,但此像素的所有时间序列s = na

library(raster)
r1 <- r2 <- r3 <- raster(ncol=10, nrow=10)
r1[] <- runif(ncell(r1))
r2[] <- runif(ncell(r2)) / 2
r3[] <- runif(ncell(r3)) * 1.5
s <- stack(r1, r2, r3)


 r11 <- r22 <- r33 <- raster(ncol=10, nrow=10)
 r11[] <- runif(ncell(r11))
 r22[] <- runif(ncell(r22)) / 2
 r33[] <- runif(ncell(r33)) * 1.5
 g <- stack(r11, r22, r33)

If any pixel in g is >= 5, make the corresponding pixel in s = NA

which can be done like this:

  s[g >= 5]= NA

but also all time series of this pixel in s = NA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2025-02-20 15:39:21

让我们用“ Terra”(替换“栅格”)来做到这一点。

示例数据

library(terra)
set.seed(0)
s <- rast(ncol=10, nrow=10, nlyr=3, vals=rep(1:100, 3))
g <- rast(ncol=10, nrow=10, nlyr=3, vals=sample(8, 300, replace=TRUE))

s中的所有值设置为na,如果g中的单元格大于或等于其任何层中的5个。

g5 <- any(g >= 5)  
x <- mask(s, g5, maskvalue=1)

Let's do this with "terra" (the replacement of "raster").

Example data

library(terra)
set.seed(0)
s <- rast(ncol=10, nrow=10, nlyr=3, vals=rep(1:100, 3))
g <- rast(ncol=10, nrow=10, nlyr=3, vals=sample(8, 300, replace=TRUE))

Set all values in s to NA if a cell in g is larger than or equal to 5 in any of its layers.

g5 <- any(g >= 5)  
x <- mask(s, g5, maskvalue=1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文