rollup: rollup add a total row to a dataframe

Description Usage Arguments Value Examples

View source: R/dplyrExtend.R

Description

This function is inteded to extend the 'summarise' function in dplyr by binding (using 'bind_row') a "Total" row to the dataframe. This is especially useful when generating reports that require a summary line. It also appropriatly scales everything to make sure that aggregate functions such as 'mean' are taken on the raw data and not the aggregate data.

Usage

1
rollup(.data, ..., label = "Total")

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.

label

what should the total row be labeled as? Only valid if the first grouping variables is a factor or character.

Value

An object of the same class as .data. One grouping level will be dropped.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
library(dplyr)

# cyl is a number so it's not possible to label the "Total Row"
mtcars %>%
  group_by(cyl) %>%
  rollup(mean = mean(disp), n = n())

# Casting cyl to a character allows automatic labeling of the final row.
mtcars %>%
  mutate(cyl = as.character(cyl)) %>%
  group_by(cyl) %>%
  rollup(sum = sum(disp),
         mean = mean(disp),
         sd = sd(disp),
         n = n())

# Set label = NA to not label the last row (same as if it's a number)
mtcars %>%
  mutate(cyl = as.character(cyl)) %>%
  group_by(cyl) %>%
  rollup(sum = sum(disp),
         mean = mean(disp),
         sd = sd(disp),
         n = n(),
         label = NA)

West-End-Statistics/r-library-vakdr documentation built on Dec. 18, 2021, 7:16 p.m.