【问题标题】:Selecting the maximum value for each raster layer in R为 R 中的每个栅格图层选择最大值
【发布时间】:2020-01-31 02:11:58
【问题描述】:

我目前正在尝试为我拥有的每个栅格图层选择最大值。我已经这样做了:

library(raster)
Model4 <- brick("MaxPrecCCCMACanESM2rcp45.nc", var="onedaymax")
subset<-Model4[[1:90]]
subset

class       : RasterStack 
dimensions  : 64, 128, 8192, 90  (nrow, ncol, ncell, nlayers)
resolution  : 2.8125, 2.789327  (x, y)
extent      : -181.4062, 178.5938, -89.25846, 89.25846  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
names       : X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, ... 

求出每个栅格图层的最大值,我试过这个:

Maxprec <- max(subset, na.rm=TRUE)
Maxprec

class       : RasterLayer 
dimensions  : 64, 128, 8192  (nrow, ncol, ncell)
resolution  : 2.8125, 2.789327  (x, y)
extent      : -181.4062, 178.5938, -89.25846, 89.25846  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 1.690237, 363.9818  (min, max)

但是,我不确定这是否正确选择了每一层的最大值 - 鉴于显示的最小值(即 1.69),我不确定这是否捕获了最大值?最终,应该只有 90 个最大值(即 90 层每层最多 1 个)

任何帮助将不胜感激!

谢谢,

【问题讨论】:

    标签: r max r-raster


    【解决方案1】:

    如果您不确定某项工作是否正常,请做一个小测试。这是三个小栅格,我知道每个栅格的最大值:

    > r1 = raster(matrix(1:12, 3,4))
    > r2 = raster(matrix(12:23, 3,4))
    > r3 = raster(matrix(23:34, 3,4))
    

    如果您愿意,可以将其制成砖块或堆栈:

    > b = brick(r1,r2,r3)
    

    然后要找到每一层中的最大值,请使用unstack 创建一个列表,您可以使用任何标准的 R“应用”函数系列对其进行循环。在这种情况下,使用sapply

    > sapply(unstack(b), function(r){max(values(r))})
    [1] 12 23 34
    

    我的三层中的每一层都有最大值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2018-06-14
      相关资源
      最近更新 更多