Learn R Programming

pxmake (version 0.17.0)

px_add_totals: 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 px px_add_totals(x, value, na.rm = TRUE, validate = TRUE)

Value

A px object

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.

See Also

px_elimination

Examples

Run this code
# 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

Run the code above in your browser using DataLab