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 triedlabels(df), but that only gives me the variable names; andstr(df$var)is also is not telling me label. - Using a function from the
havenpackage,attributes(df$var)gives me levels and class, but not variable label.
Am I missing something here?
1 Answer 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)
havendocs (assuming this is your package), Variable labels are stored in the "label" attribute of each variable. Check:attributes(df$col1).str(DF)to look around for the labels more.havenpackage andattributes(df$var), which gives me levels and class, but not variable label.