sum_total: Sum over one dimension of a data frame

View source: R/sum_total.R

sum_totalR Documentation

Sum over one dimension of a data frame

Description

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

Usage

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

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

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)?

weight

column of the weights to be applied, if any

Value

a data frame

Author(s)

Michaja Pehl

Examples


require(dplyr)

(d <- expand.grid(
    UPPER  = LETTERS[1:2],
    lower  = letters[24:26],
    number = 1:2
) %>%
        arrange(UPPER, lower, number) %>%
        mutate(value = c(1:6, NA, 8:12)))

sum_total(d, UPPER)

sum_total(d, lower, name = 'sum over lower', na.rm = FALSE)

(e <- tibble(
    item = c('large', 'medium', 'small'),
    specific.value = c(1, 10, 100),
    size = c(1000, 100, 1)))

sum_total(e, item, value = specific.value, name = 'Average', weight = size)


pik-piam/quitte documentation built on April 26, 2024, 12:58 a.m.