Help with converting monthly tsibble to quarterly tsibble

Hello,

I have a tsibble with monthly data and I want to convert it to quarterly data. I am not sure where I am going wrong. Thanks for your help!

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 <- df %>%
  mutate(date = yearquarter(date))%>%
  #as_tsibble(index = date)%>%
  group_by(yearquarter(date))%>% 
  summarise(sales = sum(sales))

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)

Thank you @andresrcs ! For some reason when I create reprex for my tsibble, my date changes to character type. That's why my sample data had character date type. Is there a better way to retain the data type when using datapasta.

index_by(year_quarter = ~ yearquarter(.)) is the key here based on your suggestion. Thank you! I was trying to do yearquarter(date) and it wasn't working.

Sorry but this is considered cross-posting and it is against the forum policies, also, to go from quarter to monthly you would be basically madeing up data so you would need to specify what would be the madeing up criteria.

Understood! I took that question off from this post now.
But thank you for your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.