Learn R Programming

tsibble (version 0.1.5)

tsummarise: Aggregate over calendar periods

Description

It computes summary statistics for a tsibble over calendar periods, usually used in combination of group_by.

Usage

tsummarise(.data, ...)

tsummarise_all(.data, ..., .funs)

tsummarise_if(.data, ..., .predicate, .funs)

tsummarise_at(.data, ..., .vars, .funs)

tsummarize(.data, ...)

tsummarize_all(.data, ..., .funs)

tsummarize_if(.data, ..., .predicate, .funs)

tsummarize_at(.data, ..., .vars, .funs)

Arguments

.data

A data frame (of tbl_ts class).

...

Name-value pairs of expressions. The index variable must be present in the first name-value pair, with an index function. The remaining components work like summarise(). For the scoped variants like _all(), _at(), _if(), additional arguments for the function call in .funs will be ignored in .... The index functions that can be used, but not limited:

.funs

List of function calls generated by funs(), or a character vector of function names, or simply a function.

Bare formulas are passed to rlang::as_function() to create purrr-style lambda functions. Note that these lambda prevent hybrid evaluation from happening and it is thus more efficient to supply functions like mean() directly rather than in a lambda-formula.

.predicate

A predicate function to be applied to the columns or a logical vector. The variables for which .predicate is or returns TRUE are selected. This argument is passed to rlang::as_function() and thus supports quosure-style lambda functions and strings representing function names.

.vars

A list of columns generated by vars(), or a character vector of column names, or a numeric vector of column positions.

Details

The rightmost grouping level will be dropped.

See Also

dplyr::summarise_all

Examples

Run this code
# NOT RUN {
# Monthly counts across Sensors
data(pedestrian)
monthly_ped <- pedestrian %>% 
  group_by(Sensor) %>% 
  tsummarise(
    Year_Month = yearmonth(Date_Time), # Year_Month will be the new index
    Max_Count = max(Count),
    Min_Count = min(Count)
  )
monthly_ped
index(monthly_ped)

# Annual trips by Region and State ----
data(tourism)
tourism %>% 
  group_by(Region | State) %>% 
  tsummarise(Year = lubridate::year(Quarter), Total = sum(Trips))
# scoped variants ----
tsbl <- tsibble(
  qtr = rep(yearquarter(seq(2010, 2012.25, by = 1 / 4)), 3),
  group = rep(c("x", "y", "z"), each = 10),
  a = rnorm(30),
  b = rnorm(30),
  c = rnorm(30),
  key = id(group), index = qtr
)
tsbl %>% 
  group_by(group) %>% 
  tsummarise_all(year = lubridate::year(qtr), .funs = mean)
tsbl %>% 
  group_by(group) %>% 
  tsummarise_if(
     year = lubridate::year(qtr), 
     .predicate = is.numeric, .funs = sum
  )
# additional arguments need putting into the `.funs`
tsbl %>% 
  group_by(group) %>% 
  tsummarise_at(
     year = lubridate::year(qtr), 
     .vars = c("a", "c"), .funs = function(x) median(x, na.rm = TRUE)
  )
# }

Run the code above in your browser using DataLab