I have a .dta file which has variable labels for each variable. Once imported to R, how can I view these labels?

1.7k Views Asked by At

I imported a .dta (Stata file format) into R, but it looks like the variable labels did not get imported along with the variable names.

  • Using foreign::read.dta, I tried labels(df), but that only gives me the variable names; and str(df$var) is also is not telling me label.
  • Using a function from the haven package, attributes(df$var) gives me levels and class, but not variable label.

Am I missing something here?

1

There are 1 best solutions below

0
laura bmw On

To see variable labels in R, it depends on how the Stata file is imported. Just using the foreign package (command read.dta) does not import variable labels.

Use the haven package to import the Stata file (read_dta command). Using the attributes command via haven package (@parfait) will give you format, class, and levels, in addition to variable label. However, if you only want to see the variable labels, then use the var_lab command from the expss package.

    library(haven)
    df <- read_dta(file="df.dta")
    library(expss)
    lapply(df, var_lab)
    # OR
    var_lab(df$var)