Tom
Tom

Reputation: 2341

Getting the labels of the column names (hmisc)

I have used the Hmisc library to attach labels to column names, which you could do as follows.

example

library(Hmisc)
label(mydata$myvar) <- "Variable label for variable myvar" 

Results:

enter image description here

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

Answers (1)

akrun
akrun

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

Related Questions