Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

fsummarise

Fast Summarise


Description

fsummarize is a much faster version of dplyr::summarise, when used together with the Fast Statistical Functions.

Usage

fsummarise(.data, ..., keep.group_vars = TRUE)
smr(.data, ..., keep.group_vars = TRUE)        # Shortcut

Arguments

.data

a (grouped) data frame or named list of columns. Grouped data can be created with fgroup_by or dplyr::group_by.

...

name-value pairs of summary functions. The name will be the name of the variable in the result. Functions when applied to a vector need to return a scalar. For fast performance and weighted aggregation use the Fast Statistical Functions.

keep.group_vars

logical. FALSE removes grouping variables after computation.

Value

If .data is grouped by fgroup_by or dplyr::group_by, the result is a data frame of the same class and attributes with rows reduced to the number of groups. If .data is not grouped, the result is a data frame of the same class and attributes with 1 row.

Note

Functions other than the Fast Statistical Functions cannot be passed weight vectors as fsummarize only splits the .data, not any arguments passed to functions. If you want to summarize data with a weighted function you created yourself, use data.table.

See Also

Examples

# Simple use
fsummarise(mtcars, mean_mpg = fmean(mpg),
                   sd_mpg = fsd(mpg))

# Using base functions (not a big difference without groups)
fsummarise(mtcars, mean_mpg = mean(mpg),
                   sd_mpg = sd(mpg))

# Grouped use
library(magrittr)
mtcars %>% fgroup_by(cyl) %>%
  fsummarise(mean_mpg = fmean(mpg),
             sd_mpg = fsd(mpg))

# This is still efficient but quite a bit slower on large data (many groups)
mtcars %>% fgroup_by(cyl) %>%
  fsummarise(mean_mpg = mean(mpg),
             sd_mpg = sd(mpg))

# Weighted aggregation
mtcars %>% fgroup_by(cyl) %>%
  fsummarise(w_mean_mpg = fmean(mpg, wt),
             w_sd_mpg = fsd(mpg, wt))

 
## Can also group with dplyr::group_by, but at a conversion cost, see ?GRP
library(dplyr)
mtcars %>% group_by(cyl) %>%
  fsummarise(mean_mpg = fmean(mpg),
             sd_mpg = fsd(mpg))

# Again less efficient...
mtcars %>% group_by(cyl) %>%
  fsummarise(mean_mpg = mean(mpg),
             sd_mpg = sd(mpg))

collapse

Advanced and Fast Data Transformation

v1.5.3
GPL (>= 2) | file LICENSE
Authors
Sebastian Krantz [aut, cre], Matt Dowle [ctb], Arun Srinivasan [ctb], Laurent Berge [ctb], Dirk Eddelbuettel [ctb], Josh Pasek [ctb], Kevin Tappe [ctb], R Core Team and contributors worldwide [ctb], Martyn Plummer [cph], 1999-2016 The R Core Team [cph]
Initial release
2021-03-05

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.