Using Stata Variable Labels in R

前端 未结 5 2001
谎友^
谎友^ 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 14:46

    When using the haven package:

    if the data set you are importing is heavy, viewing the data in Rstudio might not be optimal.

    You can instead get a data.frame with column names, column labels and an indicator for whether the column is labelled:

    d <- read_dta("your_stata_data.dta") 
    
    vars <- data.frame(
                       "name" = names(d),
                       "label" = sapply(d, function(x) attr(x, "label"))  %>% as.character(),
                       "labelled" = sapply(d, is.labelled) )
    

    Note: need to use as.characted in order to avoid NULL in the labels being dropped and therefore ending up with different vector lengths.

提交回复
热议问题