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.

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

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
)
#> <labelled<double>[8]>
#> [1]  1  1  1  2  2  2 25 27
#>
#> Labels:
#>  value           label
#>      1 Pre-college age
#>      2     College age