Skip to contents

Create a labelled vector from an unlabelled vector using lbl_relabel() syntax, allowing for the grouping of multiple values into a single label. Values not assigned a label remain unlabelled.

Usage

lbl_define(x, ...)

Arguments

x

An unlabelled vector

...

Arbitrary number of two-sided formulas.

The left hand side should be a label placeholder created with lbl().

The right hand side should be a function taking .val that evaluates to TRUE for all cases that should receive the label specified on the left hand side.

Can be provided as an anonymous function or formula. See Details section.

Value

A labelled vector

Details

Several lbl_*() functions include arguments that can be passed a function of .val and/or .lbl. These refer to the existing values and labels in the input vector, respectively.

Use .val to refer to the values in the vector's value labels. Use .lbl to refer to the label names in the vector's value labels.

Note that not all lbl_*() functions support both of these arguments.

See also

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

Examples

age <- c(10, 12, 16, 18, 20, 22, 25, 27)

# Group age values into two label groups.
# Values not captured by the right hand side functions remain unlabelled
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