Learn R Programming

ipumsr (version 0.4.5)

lbl_define: Define labels for an unlabelled vector

Description

Creates a labelled vector from an unlabelled atomic vector using lbl_relabel syntax, which allows grouping multiple values into a single labelled value. Values not assigned a label will remain unlabelled.

Usage

lbl_define(x, ...)

Arguments

x

An unlabelled atomic vector

...

Two-sided formulas where the left hand side is a label placeholder (created with the lbl function) and the right hand side is a function that returns a logical vector that indicates which existing values should be assigned that labeled value. The right hand side is passed to a function similar to as_function, so also accepts quosure-style lambda functions (that use values .val and .lbl). See examples for more information.

Value

A haven::labelled vector

See Also

Other lbl_helpers: lbl_add(), lbl_clean(), lbl_collapse(), lbl_na_if(), lbl_relabel(), lbl(), zap_ipums_attributes()

Examples

Run this code
# NOT RUN {
age <- c(10, 12, 16, 18, 20, 22, 25, 27)

# Note that values not assigned a new labelled value remain unchanged
lbl_define(
  age,
  lbl(1, "Pre-college age") ~ .val < 18,
  lbl(2, "College age") ~ .val >= 18 & .val <= 22
)
# }

Run the code above in your browser using DataLab