recover_labs: Recover lost labels from same-structured tibble with labels

View source: R/utils.R

recover_labsR Documentation

Recover lost labels from same-structured tibble with labels

Description

Many operations, such as rbind, cbind or its tidyverse analogues, strips out the variable labels. Use recover_labs() to bring them back from a tibble or data.frame where they are last present. The function attempts a few checks for new and original data compatibility. Note that the infix operator is available for a quick and self-explanatory usage.

Usage

recover_labs(new_data, orig_data)

new_data %labs_from% orig_data

Arguments

new_data

new dataframe that you want to recover the labs for

orig_data

original dataframe with variable labels present

Value

Tibble or data.frame with variable labels restored.

Examples

# make labels for iris dataset, labels will be colnames
# with dot replaced for whitespace
iris_with_labs <- as.data.frame(mapply(function(x, y) {
  attr(x, "label") <- y
  return(x)
}, iris, gsub("\\.", " ", colnames(iris)), SIMPLIFY = FALSE))

iris_with_recovered_labs <- recover_labs(iris, iris_with_labs)
iris_with_recovered_labs_infix <- iris %labs_from% iris_with_labs

# check
get_labs_df(iris_with_recovered_labs)
get_labs_df(iris_with_recovered_labs_infix)


scholaempirica/reschola documentation built on Feb. 1, 2024, 12:26 a.m.