sumr: Summarise with explicit group structure

Description Usage Arguments Examples

View source: R/sumr.R

Description

Use the superpowered dplyr::summarise() with explicit grouping and checks for length-one results per group.

Usage

1
2
3
4
5
6
7
sumr(.data, ...)

sumr_keep(.data, ...)

sumr_peel(.data, ...)

sumr_rowwise(.data, ...)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.

...

<data-masking> Name-value pairs of summary functions. The name will be the name of the variable in the result.

The value can be:

  • A vector of length 1, e.g. min(x), n(), or sum(is.na(y)).

  • A vector of length n, e.g. quantile().

  • A data frame, to add multiple columns from a single expression.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
library(dplyr, warn.conflicts = FALSE)

# default drops grouping after sumr
starwars %>%
  group_by(species) %>%
  sumr(mass = mean(mass))

# can peel back grouping structure
# with each sumr_peel()
starwars %>%
  group_by(gender, species) %>%
  sumr_peel(mass = mean(mass, na.rm = TRUE)) %>%
  sumr_peel(mass = mean(mass, na.rm = TRUE))

# can also keep grouping structure
starwars %>%
  group_by(species) %>%
  sumr_keep(mass = mean(mass))

paleolimbot/sumr documentation built on Dec. 31, 2020, 1:13 a.m.