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.
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)
)
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).
A solution with purrr package from tidyverse:
df %>% map_chr(~attributes(.)$label)