R: Assign variable labels of data frame columns

You can do this by creating a list from the named vector of var.labels and assigning that to the label values. I’ve used match to ensure that values of var.labels are assigned to their corresponding column in data even if the order of var.labels is different from the order of the data columns.

library(Hmisc)

var.labels = c(age="Age in Years", sex="Sex of the participant")

label(data) = as.list(var.labels[match(names(data), names(var.labels))])

label(data)
                     age                      sex 
          "Age in Years" "Sex of the participant" 

Original Answer

My original answer used lapply, which isn’t actually necessary. Here’s the original answer for archival purposes:

You can assign the labels using lapply:

label(data) = lapply(names(data), function(x) var.labels[match(x, names(var.labels))])

lapply applies a function to each element of a list or vector. In this case the function is applied to each value of names(data) and it picks out the label value from var.labels that corresponds to the current value of names(data).

Reading through a few tutorials is a good way to get the general idea, but you’ll really get the hang of it if you start using lapply in different situations and see how it behaves.

Leave a Comment