copy_labels: Copy Labels from One Data Frame to Another
Description
Copies variable labels, value labels, and tagged NA metadata from a source
data frame to a target data frame. This is essential after dplyr operations
like dplyr::filter(), dplyr::select(), or dplyr::mutate() which can
strip label attributes.
Usage
copy_labels(data, source)
Value
The target data frame with labels copied from the source. Only
columns present in both data frames are affected. Columns only in
the target are left unchanged.
Arguments
data
The target data frame (e.g., after filtering or subsetting).
source
The source data frame with the original labels.
Details
The following attributes are copied for each shared column:
"label" — variable label
"labels" — value labels
"na_tag_map" — tagged NA mapping
"na_tag_format" — tagged NA format (spss/stata/sas)
# Labels are lost after dplyr operationsdata_subset <- dplyr::filter(survey_data, age >= 18)
# Restore themdata_subset <- copy_labels(data_subset, survey_data)