Skip to contents

Remove labels that do not appear in the data. When converting labelled values to a factor, this avoids the creation of additional factor levels.

Usage

lbl_clean(x)

Arguments

x

A labelled vector

Value

A labelled vector

See also

Other lbl_helpers: lbl(), lbl_add(), lbl_define(), lbl_na_if(), lbl_relabel(), zap_ipums_attributes()

Examples

x <- haven::labelled(
  c(1, 2, 3, 1, 2, 3, 1, 2, 3),
  c(Q1 = 1, Q2 = 2, Q3 = 3, Q4 = 4)
)

lbl_clean(x)
#> <labelled<double>[9]>
#> [1] 1 2 3 1 2 3 1 2 3
#> 
#> Labels:
#>  value label
#>      1    Q1
#>      2    Q2
#>      3    Q3

# Compare the factor levels of the normal and cleaned labels after coercion
as_factor(lbl_clean(x))
#> [1] Q1 Q2 Q3 Q1 Q2 Q3 Q1 Q2 Q3
#> Levels: Q1 Q2 Q3

as_factor(x)
#> [1] Q1 Q2 Q3 Q1 Q2 Q3 Q1 Q2 Q3
#> Levels: Q1 Q2 Q3 Q4