Convenient way to access variables label after importing Stata data with haven

Heisenberg picture Heisenberg · Jan 15, 2016 · Viewed 7.5k times · Source

In R, some packages (e.g. haven) insert a label attributes to variables (e.g. haven), which explains the substantive name of the variable. For example, gdppc may have the label GDP per capita.

This is extremely useful, especially when importing data from Stata. However, I still struggle to know how to use this in my workflow.

  1. How to quickly browse the variable and the variable label? Right now I have to do attributes(df$var), but this is hardly convenient to get a glimpse (a la names(df))

  2. How to use these labels in plots? Again, I can use attr(df$var, "label") to access the string label. However, it seems cumbersome.

Is there any official way to use these labels in a workflow? I can certainly write a custom function that wraps around the attr, but it may break in the future when packages implement the label attribute differently. Thus, ideally I'd want an official way supported by haven (or other major packages).

Answer

Irina picture Irina · Apr 13, 2017

A solution with purrr package from tidyverse:

df %>% map_chr(~attributes(.)$label)