Skip to contents

Aggregate time series data preserving the time series

Usage

time_aggregate(
  df = i_timestamped,
  ...,
  .groups = NULL,
  .cols = NULL,
  .fns = NULL
)

Arguments

df

an optionally grouped time series. Grouping should not include the time column. The grouping works differently from dplyr::summarise in that the last level of non-time groups is lost in this operation, so the subgroup you wish to aggregate should be included in the grouping.

...

A set of dplyr::summarise statements, or additional parameters for .fns

.groups

as per dplyr::summarise

.cols

Optional tidyselect column specification for dplyr::across. if .fns is given and the .cols parameter is not specified then the columns to summarise are automatically identified. In doing this any Date columns are dropped. If this in not what you want then .cols or ... must be given

.fns

Optional a set of function specifications as per dplyr::across

Value

the summarised time series preserving the time column, and with the grouping structure involving one fewer levels that the input

Examples

growthrates::england_covid %>%
  time_aggregate(count = sum(count), denom = sum(denom)) %>%
  dplyr::glimpse()
#> Rows: 1,410
#> Columns: 3
#> $ time  <time_prd> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1…
#> $ count <dbl> 1, 0, 0, 1, 18, 0, 1, 0, 0, 3, 1, 1, 3, 1, 1, 0, 0, 0, 1, 0, 0, …
#> $ denom <dbl> 19, 0, 0, 19, 342, 0, 19, 0, 0, 57, 19, 19, 57, 19, 19, 0, 0, 0,…

growthrates::england_covid %>%
  time_aggregate(.fns=mean) %>%
  dplyr::glimpse()
#> Rows: 1,410
#> Columns: 3
#> $ time  <time_prd> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1…
#> $ count <dbl> 0.05263158, 0.00000000, 0.00000000, 0.05263158, 0.94736842, 0.00…
#> $ denom <dbl> 1, 0, 0, 1, 18, 0, 1, 0, 0, 3, 1, 1, 3, 1, 1, 0, 0, 0, 1, 0, 0, …