Reputation: 2341
I have used the Hmisc
library to attach labels to column names, which you could do as follows.
library(Hmisc)
label(mydata$myvar) <- "Variable label for variable myvar"
Results:
It would be possible to set the labels for an entire df
using a df called Lbl
with in one column the names and one column the labels;
Varcode Variables
1 P Power
2 H Happiness
as follows:
# set labels
for (i in seq_len(nrow(Lbl))) {
Hmisc::label(df2[[Lbl$Varcode[i]]]) <- Lbl$Variables[i]
}
My question however is, if I have a labeled dataframe, how would I get them out (ie. reverse the command)?
Upvotes: 2
Views: 4273
Reputation: 887088
Here we extract the labels
from each column and stack
it to a data.frame with two columns
setNames(stack(lapply(df2, label))[2:1], c("Varcode", "Variables"))
Upvotes: 4