lbl_relabel: Relabel labelled values

Description Usage Arguments Value See Also Examples

View source: R/lbl_helpers.r

Description

Converts values to a new value (that may or may not exist) based on their label and value in a labelled vector. Ignores any value that does not have a label.

Usage

1

Arguments

x

A labelled vector

...

Two-sided formulas where the left hand side is a label placeholder (created with the lbl function) or a value that already exists in the data and the right hand side is a function that returns a logical vector that indicates which labels should be relabeled. 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_define(), lbl_na_if(), lbl(), zap_ipums_attributes()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
x <- haven::labelled(
  c(10, 10, 11, 20, 30, 99, 30, 10),
  c(Yes = 10, `Yes - Logically Assigned` = 11, No = 20, Maybe = 30, NIU = 99)
)

lbl_relabel(
  x,
  lbl(10, "Yes/Yes-ish") ~ .val %in% c(10, 11),
  lbl(90, "???") ~ .val == 99 | .lbl == "Maybe"
)

# If relabelling to labels that already exist, don't need to specify both label
# and value:
# If just bare, assumes it is a value:
lbl_relabel(x, 10 ~ .val == 11)
# Use single argument to lbl for the label
lbl_relabel(x, lbl("Yes") ~ .val == 11)
# Or can used named arguments
lbl_relabel(x, lbl(.val = 10) ~ .val == 11)

ipumsr documentation built on July 22, 2020, 1:06 a.m.