问题
I would like to use values from a character vector that I created as label attributes for a set of variables in a dataframe.
I thought this simple solution should work, yet it does not:
x <- rep("text", time=19) %>%
paste(1:19, sep = " ") #character vector with names of label attributes I want
attr(mydataframe[var_names], "label") <- x #var_names and x have the same length
Thanks for your help!
回答1:
Hmisc supports column labels. Using the built in data frame anscombe
having 8 columns:
library(Hmisc)
x <- paste("label", i)
for(i in seq_along(anscombe)) label(anscombe[[i]]) <- x[i]
Label(anscombe)
giving:
label(x1) <- 'label 1'
label(x2) <- 'label 2'
label(x3) <- 'label 3'
label(x4) <- 'label 4'
label(y1) <- 'label 5'
label(y2) <- 'label 6'
label(y3) <- 'label 7'
label(y4) <- 'label 8'
来源:https://stackoverflow.com/questions/52116172/in-r-add-metadata-from-a-vector-to-a-set-of-columns-of-a-dataframe