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

詹卡

我正在尝试根据特定时间段对每月栅格时间序列进行子集化,在这种情况下,我只想要从“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 的所有栅格的子集。根据您的需要,您可以分别对每个 Oct 到 Feb 时间序列进行子集:

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)))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章