tsibble needs proper dates to work, not character variables.
library(tsibble)
library(dplyr)
library(lubridate)
df <- data.frame(
date = c("1996 Jan",
"1996 Feb","1996 Mar","1996 Apr","1996 May",
"1996 Jun","1996 Jul","1996 Aug","1996 Sep",
"1996 Oct","1996 Nov","1996 Dec","1997 Jan",
"1997 Feb","1997 Mar","1997 Apr","1997 May",
"1997 Jun","1997 Jul","1997 Aug","1997 Sep",
"1997 Oct","1997 Nov","1997 Dec"),
sales = c(376,393,524,
525,544,516,428,451,484,481,407,453,
484,468,584,583,594,578,546,570,710,622,
547,722)
)
df %>%
mutate(date = ym(date)) %>%
as_tsibble(index = date) %>%
index_by(year_quarter = ~ yearquarter(.)) %>%
summarise(sales = sum(sales))
#> # A tsibble: 8 x 2 [1Q]
#> year_quarter sales
#> <qtr> <dbl>
#> 1 1996 Q1 1293
#> 2 1996 Q2 1585
#> 3 1996 Q3 1363
#> 4 1996 Q4 1341
#> 5 1997 Q1 1536
#> 6 1997 Q2 1755
#> 7 1997 Q3 1826
#> 8 1997 Q4 1891
Created on 2021-03-26 by the reprex package (v1.0.0.9002)