R栅格堆栈子集,按日期排列

问题描述 投票:0回答:1

我试图根据一个特定的时间段对一个月度栅格时间序列进行子集,在这种情况下,我只需要从'n'年的10月到'n+1'年的2月的栅格(即10月、11月、12月、1月、2月)。然后我将分析这些子集:例如:在下面的例子中,我应该得到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

请你帮忙

r date time-series subset raster
1个回答
2
投票

你目前的代码子集了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)))
© www.soinside.com 2019 - 2024. All rights reserved.