1
library(tsibble)
library(dplyr)
example <- as_tsibble(ts(rep(1,10),frequency = 12,start=2010))

example %>%
  index_by(quarter = ~ yearquarter(.)) %>%
  summarize(value=sum(value))

# A tsibble: 4 x 2 [1Q]
#  quarter value
#    <qtr> <dbl>
#1 2010 Q1     3
#2 2010 Q2     3
#3 2010 Q3     3
#4 2010 Q4     1

Here the 1 is unwanted, as sum isn't set to na.rm=TRUE. I'd like a readable way, relying on tsibbles only (ie without any time filtering on the high frequency index, nor adding manual month observations), to make strict aggregations on tsibble. Ie to ensure the uncomplete low-frequencies values are NA. There, the strict approach would be 2010 Q4 to be NA, not 1.

There appears not to be any fill_gaps argument within the package tsibble that force the filling to go until the end of the lower frequency, that would allow something like

example %>%
  index_by(quarter = ~ yearquarter(.)) %>%
  fill_gaps() %>%
  summarize(value=sum(value))

to give the right strict aggregation involving a NA.

Arnaud Feldmann
  • 761
  • 5
  • 17
  • what exactly would be your desired result? `fill_gaps` should complete the year 2010 and hence the sum of Q4 should be NA because it contains NAs then? – mnist Sep 15 '21 at 13:03
  • @mnist my desired result would be the value of the Q4 to be NA, without relying on filtering nor adding arbitrary NA. – Arnaud Feldmann Sep 15 '21 at 13:07
  • The point is not about the result (I would be able to put a NA by myself) but about the way to do it, ie relying on the tsibble syntax – Arnaud Feldmann Sep 15 '21 at 13:09
  • so you basically want a complete year? – mnist Sep 15 '21 at 13:11
  • @mnist here a complete *quarter*, not year. In general, I want a way for index_by to be stricter hence to fill incomplete high-frequency index with NA values (that hence aggregate to NA low frequencies at the start and the end) – Arnaud Feldmann Sep 15 '21 at 13:17
  • Isn't summarize then not rather unrelated to your actual problem and just a potential subsequent step? If so, consider removing it to avoid confusion. – mnist Sep 15 '21 at 13:23
  • @mnist it isn't unrelated, the global topic is about a strict aggregation on tsibbles. index_by and summarize are linked function within the tsibble package (index_by is basically group_by for indexes) – Arnaud Feldmann Sep 15 '21 at 13:26

0 Answers0