Convert values to NA based on their label and value in a
labelled
vector. Ignores any value that does not have a
label.
lbl_na_if(x, .predicate)
x | A |
---|---|
.predicate | A function that takes .val and .lbl (the values and
labels) and returns TRUE or FALSE. It is passed to a function similar
to |
A haven::labelled vector
Other lbl_helpers:
lbl_add()
,
lbl_clean()
,
lbl_collapse()
,
lbl_define()
,
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_na_if(x, ~.val >= 90) #> <labelled<double>[8]> #> [1] 10 10 11 20 30 NA 30 10 #> #> Labels: #> value label #> 10 Yes #> 11 Yes - Logically Assigned #> 20 No #> 30 Maybe lbl_na_if(x, ~.lbl %in% c("Maybe")) #> <labelled<double>[8]> #> [1] 10 10 11 20 NA 99 NA 10 #> #> Labels: #> value label #> 10 Yes #> 11 Yes - Logically Assigned #> 20 No #> 99 NIU lbl_na_if(x, ~.val >= 90 | .lbl %in% c("Maybe")) #> <labelled<double>[8]> #> [1] 10 10 11 20 NA NA NA 10 #> #> Labels: #> value label #> 10 Yes #> 11 Yes - Logically Assigned #> 20 No # You can also use the more explicit function notation lbl_na_if(x, function(.val, .lbl) .val >= 90) #> <labelled<double>[8]> #> [1] 10 10 11 20 30 NA 30 10 #> #> Labels: #> value label #> 10 Yes #> 11 Yes - Logically Assigned #> 20 No #> 30 Maybe # Or even the name of a function na_function <- function(.val, .lbl) .val >= 90 lbl_na_if(x, "na_function") #> <labelled<double>[8]> #> [1] 10 10 11 20 30 NA 30 10 #> #> Labels: #> value label #> 10 Yes #> 11 Yes - Logically Assigned #> 20 No #> 30 Maybe