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)
# S3 method for class 'px'
px_add_totals(x, value, na.rm = TRUE)
Examples
# Create small px object example
x0 <- px(subset(population_gl, age == "65+"))
x0$data
#> # A tibble: 6 × 4
#> gender age year n
#> <chr> <chr> <chr> <dbl>
#> 1 male 65+ 2004 1481
#> 2 male 65+ 2014 2238
#> 3 male 65+ 2024 3116
#> 4 female 65+ 2004 1630
#> 5 female 65+ 2014 2004
#> 6 female 65+ 2024 2616
# Add total level to one variable
x1 <- px_add_totals(x0, "gender")
x1$data
#> # A tibble: 9 × 4
#> gender age year n
#> <chr> <chr> <chr> <dbl>
#> 1 Total 65+ 2004 3111
#> 2 Total 65+ 2014 4242
#> 3 Total 65+ 2024 5732
#> 4 male 65+ 2004 1481
#> 5 male 65+ 2014 2238
#> 6 male 65+ 2024 3116
#> 7 female 65+ 2004 1630
#> 8 female 65+ 2014 2004
#> 9 female 65+ 2024 2616
# Add total level to multiple variables
x2 <- px_add_totals(x0, c("gender", "age"))
x2$data
#> # A tibble: 18 × 4
#> gender age year n
#> <chr> <chr> <chr> <dbl>
#> 1 Total Total 2004 3111
#> 2 Total Total 2014 4242
#> 3 Total Total 2024 5732
#> 4 female Total 2004 1630
#> 5 female Total 2014 2004
#> 6 female Total 2024 2616
#> 7 male Total 2004 1481
#> 8 male Total 2014 2238
#> 9 male Total 2024 3116
#> 10 Total 65+ 2004 3111
#> 11 Total 65+ 2014 4242
#> 12 Total 65+ 2024 5732
#> 13 male 65+ 2004 1481
#> 14 male 65+ 2014 2238
#> 15 male 65+ 2024 3116
#> 16 female 65+ 2004 1630
#> 17 female 65+ 2014 2004
#> 18 female 65+ 2024 2616
# The name of the total level can be changed with px_elimination()
x3 <-
x0 |>
px_elimination("T") |>
px_add_totals("gender")
x3$data
#> # A tibble: 9 × 4
#> gender age year n
#> <chr> <chr> <chr> <dbl>
#> 1 T 65+ 2004 3111
#> 2 T 65+ 2014 4242
#> 3 T 65+ 2024 5732
#> 4 male 65+ 2004 1481
#> 5 male 65+ 2014 2238
#> 6 male 65+ 2024 3116
#> 7 female 65+ 2004 1630
#> 8 female 65+ 2014 2004
#> 9 female 65+ 2024 2616