lbl_add: Add labels for unlabelled values

View source: R/lbl_helpers.R

lbl_addR Documentation

Add labels for unlabelled values

Description

Add labels for values that don't already have them in a labelled vector.

Usage

lbl_add(x, ...)

lbl_add_vals(x, labeller = as.character, vals = NULL)

Arguments

x

A labelled vector

...

Arbitrary number of label placeholders created with lbl() indicating the value/label pairs to add.

labeller

A function that takes values being added as an argument and returns the labels to associate with those values. By default, uses the values themselves after converting to character.

vals

Vector of values to be labelled. If NULL, labels all unlabelled values that exist in the data.

Value

A labelled vector

See Also

Other lbl_helpers: lbl(), lbl_clean(), lbl_define(), lbl_na_if(), lbl_relabel(), zap_ipums_attributes()

Examples

x <- haven::labelled(
  c(100, 200, 105, 990, 999, 230),
  c(`Unknown` = 990, NIU = 999)
)

# Add new labels manually
lbl_add(
  x,
  lbl(100, "$100"),
  lbl(105, "$105"),
  lbl(200, "$200"),
  lbl(230, "$230")
)

# Add labels for all unlabelled values
lbl_add_vals(x)

# Update label names while adding
lbl_add_vals(x, labeller = ~ paste0("$", .))

# Add labels for select values
lbl_add_vals(x, vals = c(100, 200))

ipumsr documentation built on Sept. 12, 2024, 7:38 a.m.