How to extract column (variable) attributes/labels from R to csv or excel
Asked Answered
H

2

5

I have a rather large data set that has metadata related to each variable e.g. variable (column) name would be "qhyngage", and the label would read "age of youngest person in family".

I want to extract these labels along with the data into CSV/ xlsx format, so the end product would be the data set with the column names as the first row, then the labels as the second row etc.

I've already tried things from the "foreign" library in R such as dataset.labels <- as.data.frame(attr(dataset, "label")) but all this just returns NULL.

This is an example of what the characteristics of a column of data would look like in the R environment viewer:

qqhhtype : 'haven_labelled' num 24 13 4 1 ...
..-attr(*, "label")=chr "DV: Household type"
..-attr(*,"labels")= Named num -10 -9 -8 -7...
.. ..-attr(*,"names")=chr "[-10] Non-responding person" ...

End goal is to have a data set with all variables and observations but with an additional row that contains variable labels. (Currently just getting NULL for all labels)

Thank you so much for your help !

Heartstrings answered 19/8, 2019 at 7:16 Comment(0)
E
7

Add labels as a row like this:

# create example data
df <- data.frame(a = rep("a", 10), b = rep(1, 10))
attr(df$a, "label") <- "character var"
attr(df$b, "label") <- "numeric var"

l <- lapply(df, attr, "label") # Gives you list of the labeled variables
l <- as.data.frame(l, stringsAsFactors = F) # Convert list to dataframe
df <- rbind(l, df) # Bind the two

Then export with your favorite package to the desired format, e.g. readr::write_csv, readxl::write_excel.

Edit

In the tidyverse you would do this:

df %>%  
  map_dfc(attr, "label") %>% 
  rbind(df)

The tidyverse equivalent of rbind(), bind_rows() is more strict. Thus, if you wanted to use that, you would need to convert all variables in df to character first:

df %>%  
  map_dfc(attr, "label") %>% 
  bind_rows(mutate_all(df, as.character))
Edina answered 19/8, 2019 at 8:10 Comment(2)
This is great! Thanks for your help :)Heartstrings
Is there a way to do this within the tidyverse?Actable
K
1

Concerning @shs's answer concerning a tidyverse solution, note that map_dfc() and mutate_all() are now superseded. A solution using map() and across() would be the following:

# Example data set
df <- data.frame(a = rep("a", 10), b = rep(1, 10))
attr(df$a, "label") <- "character var"
attr(df$b, "label") <- "numeric var"
# Conversion of column labels
df %>%
  map(attr, "label") %>% 
  bind_rows(mutate(df, across(everything(), as.character)))
Keppel answered 15/5 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.