I have a data frame with lots of columns, and I want to edit columns' attributes, so that each column attribute will be based of a matched value from a lookup table.
Dummy data
df <- data.frame(id = c(1,2,3), age = c(10, 30, 55), eye_color = c("blue", "brown", "green"))
> df
# id age eye_color
# 1 1 10 blue
# 2 2 30 brown
# 3 3 55 green
If I just wanted to change the attribute of a single column df$id
, I would do:
attr(df$id, "label") <- "Person's ID"
> attr(df$id, "label")
# [1] "Person's ID"
However, I need to edit the "label" attributes of all columns, and I want to be more efficient. So my hope is to rely on a separate table that matches column names and "label" attributes. (In reality, I'd import a CSV file created manually outside of R, but for the sake of post reproducability, here's a dummy table that conveys my point. It could have been a data frame, as the object type doesn't matter):
label_dictionary <-
matrix(
c(
"id",
"Person's ID",
"age",
"Person's age when taking the survey",
"eye_color",
"Person's eye color"
),
ncol = 2,
byrow = TRUE
)
colnames(label_dictionary) <- c("variable", "label")
label_dictionary <- as.table(label_dictionary)
> label_dictionary
# variable label
# A id Person's ID
# B age Person's age when taking the survey
# C eye_color Person's eye color
My question
How could I edit the "label" attributes of all columns in my dataframe df
, based on matching values from the label_dictionary
table? (Assuming the order of values in label_dictionary$variable
doesn't necessarily match the order of colnames(df)
).
Thanks!