簡體   English   中英

R 按日期排列的柵格堆棧子集

[英]R Raster stack subset by date

我正在嘗試根據特定時間段對每月柵格時間序列進行子集化,在這種情況下,我只想要從“n”年 10 月到“n+1”年 2 月的柵格(意味着 10 月、11 月、十二月,一月,二月)。 然后我將分析這些子集:例如:在下面的情況下,我應該得到需要比較的 3 個子集(Oct00 到 Feb01、Oct01 到 Feb02、Oct02 到 Feb03)。

我正在運行下面的代碼,但它不起作用。 我還得到了分析中未考慮的月份(5 月至 9 月)

library(raster)
library(lubridate)

# create stack
r <- raster(ncol=10, nrow=10)
r <- stack(lapply(1:48, function(i) setValues(r, runif(100, -0, 1000))))

# add time dimension and names
date <- seq(as.Date('2000-01-01'),as.Date('2003-12-01'), 'months')
r <- setZ(r, date)
names(r)<-date
r

# time subsetting
sub <- subset(r, which(getZ(r) >= '2000-10-01' & (getZ(r) <= '2003-02-01')))
sub

你能幫忙嗎? 謝謝我

您當前的代碼子集從 2000-10-01 到 2003-02-01 的所有柵格。 根據您的需要,您可以分別對每個 10 月到 2 月的時間序列進行子集化:

sub1 <- subset(r, which(getZ(r) >= '2000-10-01' & (getZ(r) <= '2001-02-01')))
sub2 <- subset(r, which(getZ(r) >= '2001-10-01' & (getZ(r) <= '2002-02-01')))
sub3 <- subset(r, which(getZ(r) >= '2002-10-01' & (getZ(r) <= '2003-02-01')))

或所有年份的 10 月到 2 月的子集:

require(stringr)
##subset all months that fall between Oct to Feb
sub <- subset(r, which(substr(getZ(r),6,7)%in%c(10,11,12,01,02)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM