根据RasterLayer中的数值对RasterBrick进行子集操作。

huangapple go评论68阅读模式
英文:

Subsetting a RasterBrick based on values in a RasterLayer

问题

我有一个包含172个图层的 RasterBrick(每个图层对应不同的时间步),我们可以称之为 m

对于 m 的每个单元格,我想保留只大于所有图层的平均值的值(每个单元格有一个平均值)。然后,我想计算这个新的“筛选” RasterBrick 的平均值。

我计算了一个 RasterLayer,其中包含 m 的每个单元格的平均值:

mean_m <- mean(m, na.rm = TRUE)

然后执行了:

c <- m > mean_m

但是得到的是一个包含布尔值(0或1)的 RasterBrick。然后我尝试执行:

m[[c]]

但它仍然没有起作用。

是否有一种在 R 中使用 Raster 格式轻松完成此操作的方法?我知道我可以将 Raster* 文件转换为数据框,但我相信使用 Raster* 将极大加快我的工作流程。

非常感谢您的帮助!

英文:

I have one RasterBrick with 172 layers (every layer corresponds to a different time step), that we can call m.

For every cell of m, I would like to keep only the values that are bigger than the mean across all layers (one mean value per cell). Then, I would like to calculate the mean of this new "filtered" RasterBrick.

I calculated a RasterLayer with the mean value of each cell of m:

mean_m &lt;- mean(m, na.rm = TRUE)

And then did:

c &lt;- m &gt; mean_m

But what I get is a RasterBrick of boolean values (0 or 1). I then tried to do:

m[[c]]

It still didn't work.

Is there an easy way of doing this using the Raster format on R? I know that I could convert the Raster* files to dataframes, but I believe working with Raster* would speed up my workflow tremendously.

Thanks a lot in advance for your help!

答案1

得分: 0

以下是您要翻译的内容:

You can use "terra" for that (the replacement of "raster")

Example data

```R
library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))   

Solution

m <- mean(s)
x <- ifel(s > m, s, NA)
res <- mean(x, na.rm=TRUE)

You can also do

res2 <- app(s, \(x) mean(x[x > mean(x)]))

<details>
<summary>英文:</summary>

You can use &quot;terra&quot; for that (the replacement of &quot;raster&quot;)

Example data 

library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))


Solution 

m <- mean(s)
x <- ifel(s > m, s, NA)
res <- mean(x, na.rm=TRUE)


You can also do 

res2 <- app(s, (x) mean(x[x > mean(x)]))



</details>



huangapple
  • 本文由 发表于 2023年3月9日 21:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定