Convert data values in a labelled
vector
to NA
based on the value labels associated with that vector. Ignores
values that do not have a label.
lbl_na_if(x, .predicate)
A labelled
vector
A labelled
vector
A function taking .val
and .lbl
arguments that
returns TRUE
for all values that should be converted to NA
.
Can be provided as an anonymous function or formula. See Details section.
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.
Other lbl_helpers:
lbl()
,
lbl_add()
,
lbl_clean()
,
lbl_define()
,
lbl_relabel()
,
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)
)
# Convert labelled values greater than 90 to `NA`
lbl_na_if(x, function(.val, .lbl) .val >= 90)
# Can use purrr-style notation
lbl_na_if(x, ~ .lbl %in% c("Maybe"))
# Or refer to named function
na_function <- function(.val, .lbl) .val >= 90
lbl_na_if(x, na_function)
Run the code above in your browser using DataLab