使用 Haven 导入 Stata 数据后访问变量标签的便捷方式 [英] Convenient way to access variables label after importing Stata data with haven
问题描述
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 lanames(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)
这篇关于使用 Haven 导入 Stata 数据后访问变量标签的便捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!