sum_total: Sum over one dimension of a data frame

Description Usage Arguments Value Author(s) Examples

View source: R/sum_total.R

Description

sum_total() is a short-hand function to calculate and insert the sum of a category in a data frame.

Usage

1
2
3
sum_total(data, group, value = NA, name = "Total")

sum_total_(data, group, value = "NA", name = "Total", na.rm = TRUE)

Arguments

data

a data frame

group

column for which the sum is to be calculated

value

column of the numbers to be summed

name

entry in column group for the sum; defaults to "Total"

na.rm

logical. Should missing values (including NaN) be removed (default)?

Value

a data frame

Author(s)

Michaja Pehl

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
require(dplyr)
data <- expand.grid(
    UPPER  = LETTERS[1:2],
    lower  = letters[24:26],
    number = 1:2
) %>%
    arrange(UPPER, lower, number) %>%
    mutate(value = 1:n())
print(data)

data.totals <- data %>%
    # grouping is preserved through the summation
    group_by(UPPER) %>%
    sum_total(lower)
print(data.totals)

data.totals %>%
    filter(!(lower == "Total" & number == 2)) %>%
    # existing totals are filtered out
    sum_total_("lower")

michaja/quitte documentation built on May 22, 2019, 9:53 p.m.