Skip to contents

Sets variable labels using one of three conventions.

Usage

set_var_label(x, ..., variable = NULL, label = NULL)

Arguments

x

A survey design object or a data frame.

...

Named arguments where the name is the variable and the value is the label string. Supports !!! list splicing.

variable

A character vector of variable names. Use with label.

label

A character vector of label strings, one per element of variable.

Value

The modified object, invisibly.

Details

Convention 1 (named ...) — recommended for interactive use:

set_var_label(x, age = "Age in years", income = "Annual income")
set_var_label(x, !!!labels_list)   # list splicing

Convention 2 (named vector in ...) — useful for programmatic use:

set_var_label(x, c(age = "Age in years", income = "Annual income"))

Convention 3 (variable + label arguments) — for vector input:

vars <- c("age", "income")
lbls <- c("Age in years", "Annual income")
set_var_label(x, variable = vars, label = lbls)

Examples

d <- as_survey(nhanes_2017, ids = sdmvpsu, weights = wtint2yr,
               strata = sdmvstra, nest = TRUE)
d <- set_var_label(d, indfmpir = "Income-to-poverty ratio")

# Multiple variables
d <- set_var_label(d, bpxsy1 = "Systolic BP (1st reading)",
                      bpxdi1 = "Diastolic BP (1st reading)")