使用避风港导入Stata数据后方便地访问变量标签的方法 [英] Convenient way to access variables label after importing Stata data with haven

查看:132
本文介绍了使用避风港导入Stata数据后方便地访问变量标签的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,某些程序包(例如haven)将label属性插入变量(例如haven),这说明了变量的实质名称.例如,gdppc可能带有标签GDP per capita.

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.

这非常有用,尤其是从Stata导入数据时.但是,我仍然很难知道如何在我的工作流程中使用它.

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

  1. 如何快速浏览变量和变量标签?现在我必须要做attributes(df$var),但这很难一窥(la names(df))

  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))

如何在绘图中使用这些标签?同样,我可以使用attr(df$var, "label")访问字符串标签.但是,这似乎很麻烦.

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

在工作流程中有使用这些标签的任何正式方法吗?我当然可以编写一个环绕attr的自定义函数,但是将来当程序包以不同的方式实现label属性时,它可能会中断.因此,理想情况下,我希望使用haven(或其他主要软件包)支持的正式方式.

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).

推荐答案

tidyverse的 purrr软件包的解决方案:

A solution with purrr package from tidyverse:

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

这篇关于使用避风港导入Stata数据后方便地访问变量标签的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆