Search code examples
rattributes

Is there a way to get all the label attributes of columns in R


I have created a dataframe using the read_survey() command from the qualtRics package. The dataframe has variables (columns) that have a name (e.g. "Q1"), but they also have a label attribute (e.g. "What is your age?"). Is there a way to access all the labels and put them in a nice overview like a list or separate dataframe?

I have been able to access every label individually by using:

attributes(df$Q1)

Which gives the following outcome:

$label
                                                         Q1 
" What is your age?"

I know that I am supposed to give a reproducable dataframe to clarify my question, however I have no idea how to create these label attributes.

My desired outcome dataframe would look something like this:

Column 1           Column 2
Q1                 "What is your age?"
Q2                 "What is your name?"
etc                etc (i.e.The rest of the labels)

Or instead of a dataframe a list with all the labels.


Solution

  • Does this answer your question?

    Q1 = runif(5)
    Q2 = runif(5)
    
    attributes(Q1)$label <- c("What is your age?")
    attributes(Q2)$label <- c("What is your name?")
    
    
    df <- data.frame(Q1, Q2)
    
    purrr::map_df(df, ~attributes(.x)) %>%
      bind_cols(names = names(df), .)