Search code examples
rindexingrasterr-raster

R StackApply: create an 8-day index from daily data while accounting for year


I would like to create an index that I can use in StackApply for aggregating a daily rainfall raster stack into 8-day sums while accounting for the year. Let me illustrate with some code. Here's what a normal sequential daily data looks like:

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

Here is what it looks like if I sequence those daily values into 8-day intervals:

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

It wraps over to the following year just as it does from one month to the next (monthly wrap-over is fine). This is only a part of the problem because I would like the end product to be an index that I can use in StackApply. So, taking the 8-day example above, the index should look like this:

> 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

Note the 5 in the middle of the command, which indicates that it stops at December 31st 2001 and does not wrap over to 2002. In summary, I want to create an index that has a single integer for each 8-day interval throughout the year but that does not carry over the next year. I have a raster stack of over 20 years of daily data so this will be a very long index.

Thanks in advance for the help!


Solution

  • Perhaps a bit of a workaround but this should work:

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