Learn R Programming

mariposa (version 0.7.3)

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)

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

See Also

var_label(), val_labels()

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

Examples

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

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

Run the code above in your browser using DataLab