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.
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.haven package, attributes(df$var) gives me levels and class, but not variable label.Am I missing something here?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With