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

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

Examples

# 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
#> # A tsibble: 95 x 4 [1MONTH] #> # Keys: Sensor [4] #> Sensor Year_Month Max_Count Min_Count #> <chr> <mth> <dbl> <dbl> #> 1 Birrarung Marr 2015 Jan 5524. 1. #> 2 Birrarung Marr 2015 Feb 10121. 1. #> 3 Birrarung Marr 2015 Mar 9858. 1. #> 4 Birrarung Marr 2015 Apr 7293. 1. #> 5 Birrarung Marr 2015 May 5129. 1. #> 6 Birrarung Marr 2015 Jun 7556. 0. #> 7 Birrarung Marr 2015 Jul 11224. 1. #> 8 Birrarung Marr 2015 Aug 5684. 0. #> 9 Birrarung Marr 2015 Sep 7757. 0. #> 10 Birrarung Marr 2015 Oct 7085. 1. #> # ... with 85 more rows
index(monthly_ped)
#> Year_Month
# Annual trips by Region and State ---- data(tourism) tourism %>% group_by(Region | State) %>% tsummarise(Year = lubridate::year(Quarter), Total = sum(Trips))
#> # A tsibble: 1,463 x 4 [1YEAR] #> # Keys: Region | State [77] #> Region State Year Total #> <chr> <chr> <dbl> <dbl> #> 1 Adelaide South Australia 1998. 2226. #> 2 Adelaide South Australia 1999. 2218. #> 3 Adelaide South Australia 2000. 2418. #> 4 Adelaide South Australia 2001. 2264. #> 5 Adelaide South Australia 2002. 2275. #> 6 Adelaide South Australia 2003. 2203. #> 7 Adelaide South Australia 2004. 2437. #> 8 Adelaide South Australia 2005. 2034. #> 9 Adelaide South Australia 2006. 2225. #> 10 Adelaide South Australia 2007. 2317. #> # ... with 1,453 more rows
# 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)
#> # A tsibble: 9 x 5 [1YEAR] #> # Keys: group [3] #> group year a b c #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 x 2010. -1.20 -0.630 0.528 #> 2 x 2011. 0.139 0.420 -0.0911 #> 3 x 2012. 0.859 0.442 -0.929 #> 4 y 2010. -0.723 -0.479 -0.375 #> 5 y 2011. -0.588 0.0959 0.245 #> 6 y 2012. -0.805 -1.51 0.245 #> 7 z 2010. -0.545 -0.654 0.0372 #> 8 z 2011. 1.01 -0.114 0.888 #> 9 z 2012. -1.25 0.823 -0.290
tsbl %>% group_by(group) %>% tsummarise_if( year = lubridate::year(qtr), .predicate = is.numeric, .funs = sum )
#> # A tsibble: 9 x 5 [1YEAR] #> # Keys: group [3] #> group year a b c #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 x 2010. -4.81 -2.52 2.11 #> 2 x 2011. 0.555 1.68 -0.364 #> 3 x 2012. 1.72 0.884 -1.86 #> 4 y 2010. -2.89 -1.92 -1.50 #> 5 y 2011. -2.35 0.383 0.979 #> 6 y 2012. -1.61 -3.03 0.489 #> 7 z 2010. -2.18 -2.62 0.149 #> 8 z 2011. 4.03 -0.457 3.55 #> 9 z 2012. -2.50 1.65 -0.580
# 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) )
#> # A tsibble: 9 x 4 [1YEAR] #> # Keys: group [3] #> group year a c #> <chr> <dbl> <dbl> <dbl> #> 1 x 2010. -1.30 0.642 #> 2 x 2011. 0.265 -0.100 #> 3 x 2012. 0.859 -0.929 #> 4 y 2010. -0.623 -0.388 #> 5 y 2011. -0.646 0.163 #> 6 y 2012. -0.805 0.245 #> 7 z 2010. -0.676 0.324 #> 8 z 2011. 0.950 1.07 #> 9 z 2012. -1.25 -0.290