px_add_totals.px: Add total levels to variables

View source: R/add_totals.R

px_add_totalsR Documentation

Add total levels to variables

Description

Adds a total level, which is the sum of the figures for all other levels of the variable. NA values are ignored when calculating the sum.

The default name of the total level is 'Total', unless px_elimination is set, in which case the elimination value becomes the name of the total level.

Usage

px_add_totals(x, value, na.rm = TRUE, validate)

## S3 method for class 'px'
px_add_totals(x, value, na.rm = TRUE, validate = TRUE)

Arguments

x

A px object

value

A character vector of variables to add total levels to.

na.rm

Optional. Logical. If TRUE, NAs are removed before summing.

validate

Optional. If TRUE a number of validation checks are performed on the px object, and an error is thrown if the object is not valid. If FALSE, the checks are skipped, which can be usefull for large px objects where the check can be time consuming. Use px_validate() to manually preform the check.

Value

A px object

See Also

px_elimination

Examples

# Create small px object example
x0 <- px(subset(population_gl, age == "65+"))
x0$data

# Add total level to one variable
x1 <- px_add_totals(x0, "gender")
x1$data

# Add total level to multiple variables
x2 <- px_add_totals(x0, c("gender", "age"))
x2$data

# The name of the total level can be changed with px_elimination()
x3 <-
  x0 |>
  px_elimination("T") |>
  px_add_totals("gender")

x3$data


pxmake documentation built on April 11, 2025, 6:06 p.m.