Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created September 5, 2019 17:06
Show Gist options
  • Save DavisVaughan/c26560b8ac34e412fafbfc1fea16a99f to your computer and use it in GitHub Desktop.
Save DavisVaughan/c26560b8ac34e412fafbfc1fea16a99f to your computer and use it in GitHub Desktop.
library(slide)
library(dplyr, warn.conflicts = FALSE)
# be careful not to `library(tsibble)` as it will overwrite slide()
# quarterly data to start with
df <- tibble(
dates = seq(as.Date("2019-01-01"), as.Date("2021-01-01"), by = "1 quarter"),
yq = tsibble::yearquarter(dates)
)
df
#> # A tibble: 9 x 2
#> dates yq
#> <date> <qtr>
#> 1 2019-01-01 2019 Q1
#> 2 2019-04-01 2019 Q2
#> 3 2019-07-01 2019 Q3
#> 4 2019-10-01 2019 Q4
#> 5 2020-01-01 2020 Q1
#> 6 2020-04-01 2020 Q2
#> 7 2020-07-01 2020 Q3
#> 8 2020-10-01 2020 Q4
#> 9 2021-01-01 2021 Q1
# 2 quarters worth of information
df %>%
mutate(result = slide_index(dates, yq, ~.x, .before = 1))
#> # A tibble: 9 x 3
#> dates yq result
#> <date> <qtr> <list>
#> 1 2019-01-01 2019 Q1 <date [1]>
#> 2 2019-04-01 2019 Q2 <date [2]>
#> 3 2019-07-01 2019 Q3 <date [2]>
#> 4 2019-10-01 2019 Q4 <date [2]>
#> 5 2020-01-01 2020 Q1 <date [2]>
#> 6 2020-04-01 2020 Q2 <date [2]>
#> 7 2020-07-01 2020 Q3 <date [2]>
#> 8 2020-10-01 2020 Q4 <date [2]>
#> 9 2021-01-01 2021 Q1 <date [2]>
# daily data you want to index by quarter
df2 <- tibble(
dates = as.Date("2019-01-01") + 0:200,
yq = tsibble::yearquarter(dates)
)
df2
#> # A tibble: 201 x 2
#> dates yq
#> <date> <qtr>
#> 1 2019-01-01 2019 Q1
#> 2 2019-01-02 2019 Q1
#> 3 2019-01-03 2019 Q1
#> 4 2019-01-04 2019 Q1
#> 5 2019-01-05 2019 Q1
#> 6 2019-01-06 2019 Q1
#> 7 2019-01-07 2019 Q1
#> 8 2019-01-08 2019 Q1
#> 9 2019-01-09 2019 Q1
#> 10 2019-01-10 2019 Q1
#> # … with 191 more rows
# 2 quarters worth of information
# (note that the size of the input vector is the same as the size of the
# output vector, the daily data corresponding to a particular quarter is
# all grouped together)
df2 %>%
mutate(result = slide_index(dates, yq, ~.x, .before = 1))
#> # A tibble: 201 x 3
#> dates yq result
#> <date> <qtr> <list>
#> 1 2019-01-01 2019 Q1 <date [90]>
#> 2 2019-01-02 2019 Q1 <date [90]>
#> 3 2019-01-03 2019 Q1 <date [90]>
#> 4 2019-01-04 2019 Q1 <date [90]>
#> 5 2019-01-05 2019 Q1 <date [90]>
#> 6 2019-01-06 2019 Q1 <date [90]>
#> 7 2019-01-07 2019 Q1 <date [90]>
#> 8 2019-01-08 2019 Q1 <date [90]>
#> 9 2019-01-09 2019 Q1 <date [90]>
#> 10 2019-01-10 2019 Q1 <date [90]>
#> # … with 191 more rows
# here is where we switch to the second quarter. so we now get all of the
# data from 2019-q1 and 2019-q2
df2 %>%
mutate(result = slide_index(dates, yq, ~.x, .before = 1)) %>%
slice(85:95)
#> # A tibble: 11 x 3
#> dates yq result
#> <date> <qtr> <list>
#> 1 2019-03-26 2019 Q1 <date [90]>
#> 2 2019-03-27 2019 Q1 <date [90]>
#> 3 2019-03-28 2019 Q1 <date [90]>
#> 4 2019-03-29 2019 Q1 <date [90]>
#> 5 2019-03-30 2019 Q1 <date [90]>
#> 6 2019-03-31 2019 Q1 <date [90]>
#> 7 2019-04-01 2019 Q2 <date [181]>
#> 8 2019-04-02 2019 Q2 <date [181]>
#> 9 2019-04-03 2019 Q2 <date [181]>
#> 10 2019-04-04 2019 Q2 <date [181]>
#> 11 2019-04-05 2019 Q2 <date [181]>
# There are really only 3 distinct quarters, so maybe you only wanted 3 values
unique_quarters <- unique(df2$yq)
unique_quarters
#> [1] "2019 Q1" "2019 Q2" "2019 Q3"
# in that case, use the slightly more manual `slide_between()`
tibble(
starts = unique_quarters - 1,
stops = unique_quarters,
result = slide_between(df2$dates, df2$yq, starts, stops, ~.x)
)
#> # A tibble: 3 x 3
#> starts stops result
#> <qtr> <qtr> <list>
#> 1 2018 Q4 2019 Q1 <date [90]>
#> 2 2019 Q1 2019 Q2 <date [181]>
#> 3 2019 Q2 2019 Q3 <date [111]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment