3

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?

smci
34.2k21 gold badges118 silver badges152 bronze badges
asked Jun 28, 2019 at 17:51
7
  • 2
    Per haven docs (assuming this is your package), Variable labels are stored in the "label" attribute of each variable. Check: attributes(df$col1). Commented Jun 28, 2019 at 17:56
  • 1
    If Parfait's pointer doesn't resolve it, you may need to add more information (like the tool you used for import, maybe the Stata version). You could also try str(DF) to look around for the labels more. Commented Jun 28, 2019 at 17:58
  • Does that help stackoverflow.com/a/25850262/5784831? Commented Jun 28, 2019 at 18:03
  • @parfait - I just tried the haven package and attributes(df$var), which gives me levels and class, but not variable label. Commented Jun 28, 2019 at 18:15
  • 1
    First, it's useful to state that .dat is a Stata file format; 99% of users won't know - I added that in body and tags. Second, I believe you're talking about making sure categorical labels get imported right, right? Can you narrow the issue down to whether they're getting correctly exported from Stata to the .dta file (can you show us a snippet of .dta file?), or imported from .dta file to R? Commented Jun 28, 2019 at 18:29

1 Answer 1

1

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)
answered Jul 14, 2019 at 17:46
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.