x <- haven::labelled(
c(10, 10, 11, 20, 21, 30, 99, 30, 10),
c(
Yes = 10, `Yes - Logically Assigned` = 11,
No = 20, Unlikely = 21, Maybe = 30, NIU = 99
)
)
# Convert cases with value 11 to value 10 and associate with 10's label
lbl_relabel(x, 10 ~ .val == 11)
lbl_relabel(x, lbl("Yes") ~ .val == 11)
# To relabel using new value/label pairs, use `lbl()` to define a new pair
lbl_relabel(
x,
lbl(10, "Yes/Yes-ish") ~ .val %in% c(10, 11),
lbl(90, "???") ~ .val == 99 | .lbl == "Maybe"
)
# Collapse labels to create new label groups
lbl_collapse(x, ~ (.val %/% 10) * 10)
# These are equivalent
lbl_collapse(x, ~ ifelse(.val == 10, 11, .val))
lbl_relabel(x, 11 ~ .val == 10)
Run the code above in your browser using DataLab