Sets the label attribute on columns that already exist in data. Unlike
update_columns(), column names are not changed, so this can be used on any
data.frame, whether or not it has been renamed beforehand.
Arguments
- data
A data.frame-like object.
- dict
A data.frame-like object with at least two columns: one matching the current column names in
data, and one with human-readable labels.- names
Column name in
dictthat matches the current column names indata(default"new", which suits data already passed throughupdate_columns()).- label
Column name in
dictwith human-readable labels (default"label").
Details
Columns in data that are not listed in dict are left unchanged. Dictionary
entries that do not match any column in data are reported with a warning.
Empty labels ("") are applied, while NA labels are skipped.
Examples
dict <- data.frame(
names = c("age", "sex"),
label = c("Age (years)", "Sex"),
stringsAsFactors = FALSE
)
df <- data.frame(age = c(34, 58), sex = c("F", "M"))
df <- relabel_columns(df, dict, names = "names")
attr(df$age, "label")
#> [1] "Age (years)"
