Home > Software design >  Getting the labels of the column names (hmisc) and applying them to another dataframe
Getting the labels of the column names (hmisc) and applying them to another dataframe

Time:11-12

I have a question similar to enter image description here

I assume I will have to rewrite the previous code into a matching function, that only replaces if there is a match.

How should I match the labels with the new data frame if they exist?

CodePudding user response:

This is not super pretty, but you can use match to look up the variables that have labels and then only update those

matches <- match(names(df2), Lbl$Varcode) 
for (i in seq_along(matches)) {
  if (!is.na(matches[i]))
    label(df2[[i]]) <- Lbl$Variables[matches[i]]
}
  • Related