
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.
lbl_relabel(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.
A haven::labelled vector
Other lbl_helpers:
lbl_add()
,
lbl_clean()
,
lbl_collapse()
,
lbl_define()
,
lbl_na_if()
,
lbl()
,
zap_ipums_attributes()
# NOT RUN {
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)
# }
Run the code above in your browser using DataLab