Using Stata Variable Labels in R

前端 未结 5 2004
谎友^
谎友^ 2020-12-25 14:18

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

My problem is that the variable names are not helpful to me as they are like \"q0100,\" \"q0565,\"

5条回答
  •  长情又很酷
    2020-12-25 15:02

    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
    

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

提交回复
热议问题