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.
# After filtering, region == 4 no longer exists but its label remainsdata_subset <- dplyr::filter(survey_data, region != 4)
data_clean <- drop_labels(data_subset)