Learn R Programming

mariposa (version 0.7.3)

drop_labels: Remove Unused Value Labels

Description

Removes value labels for values that are not present in the data. This is useful after filtering or subsetting, when some categories may no longer exist but their labels remain attached.

Usage

drop_labels(data, ..., drop_na = FALSE)

Value

The data with unused labels removed.

Arguments

data

A data frame or a single vector.

...

Optional: unquoted variable names (tidyselect supported). If empty, applies to all labelled columns.

drop_na

If TRUE, also removes tagged NA labels. Default: FALSE (tagged NA labels are preserved even if no tagged NAs of that type exist).

Details

This is useful after subsetting data (e.g., filtering out a category). The removed category's label still exists on the variable, which can cause confusing output in frequency() or codebook(). drop_labels() cleans this up by keeping only labels for values actually present in the data.

By default, tagged NA labels are preserved (drop_na = FALSE) because they represent missing value types, not substantive categories.

See Also

val_labels(), copy_labels()

Other labels: copy_labels(), find_var(), set_na(), to_character(), to_label(), to_labelled(), to_numeric(), unlabel(), val_labels(), var_label()

Examples

Run this code
# After filtering, region == 4 no longer exists but its label remains
data_subset <- dplyr::filter(survey_data, region != 4)
data_clean <- drop_labels(data_subset)

Run the code above in your browser using DataLab