summarise_with_totals: Summarise a subgroup and create a summary row

View source: R/standalone-tidy-utils.R

summarise_with_totalsR Documentation

Summarise a subgroup and create a summary row

Description

Summarise and include a total row, or a row including the summary for the whole group, into a factor list. This looks and feels like a natural summarisation step, but applies the summarisation both to the subgroups and to the data ungrouped by one level. The additional group result is included as a new row. allows for a natural grouped and ungrouped summarisation

Usage

summarise_with_totals(
  .data,
  ...,
  .groups = NULL,
  .total = "Total",
  .total_first = FALSE
)

Arguments

.data

a dataframe

...

the summarisation specification

.groups

what to do with the grouping after summarisation (same as dplyr::summarise)

.total

name of the total row which will be added into a factor list.

.total_first

should the total be before or after the groups

Value

a summarised dataframe with the additional totals or group row

Examples

library(tidyverse)
diamonds %>%
 dplyr::group_by(color,cut) %>%
 summarise_with_totals(
    mpg = sprintf("%1.1f \u00B1 %1.1f", mean(price), stats::sd(price)),
    .total = "Overall"
 )

terminological/ggrrr documentation built on June 15, 2024, 6:35 a.m.