在R中使用Stata变量标签 [英] Using Stata Variable Labels in R

查看:405
本文介绍了在R中使用Stata变量标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆Stata .dta文件,我想在R中使用.

I have a bunch of Stata .dta files that I would like to use in R.

我的问题是变量名对我没有帮助,因为它们就像"q0100","q0565","q0500"和"q0202"一样.但是,它们被标记为"psu",怀孕人数",户主"和起点".

My problem is that the variable names are not helpful to me as they are like "q0100," "q0565," "q0500," and "q0202." However, they are labelled like "psu," "number of pregnant," "head of household," and "waypoint."

我希望能够抓取标签("psu",航路点"等),并将它们用作我的变量/列名,因为它们对我来说更容易使用.

I would like to be able to grab the labels ("psu," "waypoint," etc. . .) and use them as my variable/column names as those will be easier for me to work with.

是否有办法做到这一点,最好是在R中,还是通过Stata本身?我知道library(foreign)中的read.dta,但不知道它是否可以将标签转换为变量名.

Is there a way to do this, either preferably in R, or through Stata itself? I know of read.dta in library(foreign) but don't know if it can convert the labels into variable names.

推荐答案

R没有内置的方式来处理变量标签.我个人认为这是应该解决的缺点. Hmisc确实为哈希变量标签提供了一些便利,但是标签只能被该包中的函数识别. read.dta创建一个带有属性"var.labels"的data.frame,其中包含标签信息.然后,您可以据此创建数据字典.

R does not have a built in way to handle variable labels. Personally I think that this is disadvantage that should be fixed. Hmisc does provide some facilitiy for hadling variable labels, but the labels are only recognized by functions in that package. read.dta creates a data.frame with an attribute "var.labels" which contains the labeling information. You can then create a data dictionary from that.

> data(swiss)
> write.dta(swiss,swissfile <- tempfile())
> a <- read.dta(swissfile)
> 
> var.labels <- attr(a,"var.labels")
> 
> data.key <- data.frame(var.name=names(a),var.labels)
> data.key
          var.name       var.labels
1        Fertility        Fertility
2      Agriculture      Agriculture
3      Examination      Examination
4        Education        Education
5         Catholic         Catholic
6 Infant_Mortality Infant.Mortality

当然,此.dta文件没有非常有趣的标签,但您的标签应该更有意义.

Of course this .dta file doesn't have very interesting labels, but yours should be more meaningful.

这篇关于在R中使用Stata变量标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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