lbl_collapse | R Documentation |
Converts values to a new value based on their label and value in a
labelled
vector. If the newly assigned value does
not match an already existing labelled value, the smallest value's label
is used. Ignores any value that does not have a label.
lbl_collapse(x, .fun)
x |
A |
.fun |
A function that takes .val and .lbl (the values and
labels) and returns the values of the label you want to change it to.
It is passed to a function similar to |
A haven::labelled vector
Other lbl_helpers:
lbl_add()
,
lbl_clean()
,
lbl_define()
,
lbl_na_if()
,
lbl_relabel()
,
lbl()
,
zap_ipums_attributes()
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_collapse(x, ~(.val %/% 10) * 10) # Notice that 90 get's NIU from 99 even though 90 didn't have a label in original lbl_collapse(x, ~ifelse(.val == 10, 11, .val)) # But here 10 is assigned 11's label # You can also use the more explicit function notation lbl_collapse(x, function(.val, .lbl) (.val %/% 10) * 10) # Or even the name of a function collapse_function <- function(.val, .lbl) (.val %/% 10) * 10 lbl_collapse(x, "collapse_function")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.