【问题标题】:Subsetting a Rasterstack by month按月对 Rasterstack 进行子集化
【发布时间】:2020-10-27 18:33:53
【问题描述】:

我有一个 RasterStack,它有以下描述:

class      : RasterStack 
dimensions : 221, 121, 26741, 14976  (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.25  (x, y)
extent     : 14.875, 45.125, 24.875, 80.125  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 
names      : index_1979.01.01, index_1979.01.02, index_1979.01.03, index_1979.01.04, index_1979.01.05, index_1979.01.06, index_1979.01.07, index_1979.01.08, index_1979.01.09, index_1979.01.10, index_1979.01.11, index_1979.01.12, index_1979.01.13, index_1979.01.14, index_1979.01.15, ... 
min values :         46604.85,         47328.07,         48944.12,         49320.65,         49244.67,         49516.16,         49504.01,         48959.65,         48608.90,         47603.10,         47572.72,         48564.15,         49816.92,         49078.65,         48321.72, ... 
max values :         57006.81,         56968.60,         56958.67,         56976.26,         57288.55,         57535.62,         57659.48,         57581.33,         57381.65,         57052.99,         56803.95,         56854.89,         56783.50,         56739.44,         56600.52, ... 

从 1979 年 1 月 1 日到 2019 年 12 月 31 日,每天有 14 975 层。现在,我想从月份中取出 12 个堆栈,所以我想将其子集为 12 个较小的堆栈。由于我无法正确更改堆栈层的名称,因此我想到了另一种方法。我制作了一个与堆栈层具有相同字符数量的向量,前 31 个字符命名为 JAN,另外 28 FEB 等等......我是这样做的:

n<-names(stack)
nn<-substr(n,12,13)
nn<-gsub('01','JAN',nn)
nn<-gsub('02','FEB',nn)
...
nn<-gsub('12','DEC',nn).

现在我想通过这个向量 nn 对堆栈进行子集化,如下所示: sub

我希望你明白我想要做什么。 感谢您的帮助,谢谢!

【问题讨论】:

    标签: r stack subset raster


    【解决方案1】:

    你可以试试这个:

    layer_name <- names(stack)
    layer_name <- str_remove_all(layer_name, "[index_]")
    
    #install.packages("lubridate")
    library(lubridate)
    
    layer_name <- ymd(layer_name)
    
    #Create an indices to prepare it for stackApply, which takes the means for all the days of the month within each year.
    indices <- format(as.Date(layer_name, format = "%Y.%m.%d"), format = "%Y.m") 
    
    raster_mean <- stackApply(stack, indices, mean)
    
    • layer_name &lt;- names(stack) 这会从栅格中获取名称,以准备获取月份的总和或平均值
    • str_remove_all() 这将删除名称中的第一个单词 index_ 以将其过滤为日期 -ymd() 将字符转换为日期格式进行过滤
    • Indices选择年月的日期格式
    • stackApply()使用索引,从栅格中获取每个月内的所有天数并取平均值

    【讨论】:

      【解决方案2】:
      n <-names(stack)
      n <- c("index_1979.01.01", "index_1979.01.02", "index_1979.01.03", "index_1979.01.04", "index_1979.01.05", "index_1979.01.06", "index_1979.01.07", "index_1979.01.08", "index_1979.01.09", "index_1979.01.10", "index_1979.01.11", "index_1979.01.12", "index_1979.01.13", "index_1979.01.14", "index_1979.01.15")
      
      nn <- as.integer(substr(n,12,13))
      

      现在获取一月份的图层

      sjan <- stack[[nn == 1]] 
      

      或与子集

      subjan <-raster::subset(stack, which(nn==1))
      

      【讨论】:

        猜你喜欢
        • 2014-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-19
        • 1970-01-01
        相关资源
        最近更新 更多