Hi,
I am exploring the use of the tsibble packages, and I have a question.
My data is currently uses an index of yearquarter. I want to aggregate it up to year, but I want to shift the year, such that a given year goes from Q3 of one year through Q2 of the following year. Is there a way to do that?
To better explain:
library(tsibble)
library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)
If I want to take a data set, and aggregate it on the year-quarter level, I can do the following.
q_data <- tsibbledata::gafa_stock %>%
index_by(yq0 = yearquarter(Date)) %>%
summarize(close = last(Close))
head(q_data, 10)
#> # A tsibble: 10 x 2 [1Q]
#> yq0 close
#> <qtr> <dbl>
#> 1 2014 Q1 554.
#> 2 2014 Q2 572.
#> 3 2014 Q3 574.
#> 4 2014 Q4 524.
#> 5 2015 Q1 545.
#> 6 2015 Q2 521.
#> 7 2015 Q3 608.
#> 8 2015 Q4 759.
#> 9 2016 Q1 745.
#> 10 2016 Q2 692.
If I want to aggregate it on the calendar year level, I can extend the data frame above as follows:
q_data %>%
index_by(y0 = year(yq0)) %>%
summarize(close = last(close))
#> # A tsibble: 5 x 2 [1Y]
#> y0 close
#> <dbl> <dbl>
#> 1 2014 524.
#> 2 2015 759.
#> 3 2016 772.
#> 4 2017 1046.
#> 5 2018 1036.
But how do I do so if I want non-calendar years?
Created on 2019-09-12 by the reprex package (v0.3.0)
Thanks!