我想创建一个索引,我可以在StackApply中使用该索引,以将每年的降雨栅格堆栈汇总为8天的总和,同时说明年份。让我用一些代码来说明。正常的连续每日数据如下所示:
> seq.Date(as.Date("2001-11-01"), as.Date("2002-01-31"),by=1)
[1] "2001-11-01" "2001-11-02" "2001-11-03" "2001-11-04" "2001-11-05" "2001-11-06" "2001-11-07"
[8] "2001-11-08" "2001-11-09" "2001-11-10" "2001-11-11" "2001-11-12" "2001-11-13" "2001-11-14"
[15] "2001-11-15" "2001-11-16" "2001-11-17" "2001-11-18" "2001-11-19" "2001-11-20" "2001-11-21"
[22] "2001-11-22" "2001-11-23" "2001-11-24" "2001-11-25" "2001-11-26" "2001-11-27" "2001-11-28"
[29] "2001-11-29" "2001-11-30" "2001-12-01" "2001-12-02" "2001-12-03" "2001-12-04" "2001-12-05"
[36] "2001-12-06" "2001-12-07" "2001-12-08" "2001-12-09" "2001-12-10" "2001-12-11" "2001-12-12"
[43] "2001-12-13" "2001-12-14" "2001-12-15" "2001-12-16" "2001-12-17" "2001-12-18" "2001-12-19"
[50] "2001-12-20" "2001-12-21" "2001-12-22" "2001-12-23" "2001-12-24" "2001-12-25" "2001-12-26"
[57] "2001-12-27" "2001-12-28" "2001-12-29" "2001-12-30" "2001-12-31" "2002-01-01" "2002-01-02"
[64] "2002-01-03" "2002-01-04" "2002-01-05" "2002-01-06" "2002-01-07" "2002-01-08" "2002-01-09"
[71] "2002-01-10" "2002-01-11" "2002-01-12" "2002-01-13" "2002-01-14" "2002-01-15" "2002-01-16"
[78] "2002-01-17" "2002-01-18" "2002-01-19" "2002-01-20" "2002-01-21" "2002-01-22" "2002-01-23"
[85] "2002-01-24" "2002-01-25" "2002-01-26" "2002-01-27" "2002-01-28" "2002-01-29" "2002-01-30"
[92] "2002-01-31"
如果我将这些每日值按8天的时间间隔排序,就会是这样:
> seq.Date(as.Date("2001-11-01"), as.Date("2002-01-31"),by=8)
[1] "2001-11-01" "2001-11-09" "2001-11-17" "2001-11-25" "2001-12-03" "2001-12-11" "2001-12-19"
[8] "2001-12-27" "2002-01-04" "2002-01-12" "2002-01-20" "2002-01-28"
就像从一个月到下个月一样,它可以套用到下一年(每月可以套用)。这只是问题的一部分,因为我希望最终产品成为可在StackApply中使用的索引。因此,以上述8天为例,索引应如下所示:
> rep(1:12,times=c(8,8,8,8,8,8,8,5,8,8,8,7))
[1] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4
[32] 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 8 8 8 8 8 9
[63] 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 12 12 12 12 12 12 12
请注意命令中间的5,它指示它在2001年12月31日停止并且没有结束到2002年。总之,我想创建一个索引,该索引在每个8天间隔中都有一个整数全年,但这不会延续到明年。我有一个超过20年的每日数据的栅格堆栈,因此这将是一个非常长的索引。
预先感谢您的帮助!
答案 0 :(得分:0)
也许有些解决方法,但这应该可以解决:
library(dplyr)
library(lubridate)
df <- data.frame(date = seq.Date(as.Date("2000-01-01"), as.Date("2010-12-31"), "day"))
df %>%
mutate(year = year(date)) %>%
group_by(year) %>%
mutate(index = rep(seq_along(df$date), each = 8)[1:max(yday(date))]) %>%
ungroup() %>%
mutate(index = index + 46 * (year - min(year)))