Skip to contents

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)

Arguments

data

The target data frame (e.g., after filtering or subsetting).

source

The source data frame with the original labels.

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.

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)

  • "class" — vector class (e.g., haven_labelled)

Examples

# Labels are lost after dplyr operations
data_subset <- dplyr::filter(survey_data, age >= 18)

# Restore them
data_subset <- copy_labels(data_subset, survey_data)