问题
I'm trying to access the variable labels (this is the description of the variable) from an SPSS por file with the haven package. I can do it just fine with the foreign package but I'd like to use haven. Any suggestions?
# Using foreign I can get the variable labels
with_foreign <- foreign::read.spss(mydata.por)
attr(with_foreign, "variable.labels")
# With haven I get null
with_haven <- haven::read_spss(mydata.por)
attr(with_haven, "variable.labels")
# Some things I've experimented with
labelled::var_label(with_haven) # NULL
attributes(with_haven) # Not useful
as_factor(with_haven$var1) # Gives me definitions for factor levels (not what I need)
回答1:
As stated in read_spss
labels are stored as attributes of each column rather than attributes of the data.frame
. Try
lapply(with_haven, function(x) attributes(x)$label)
来源:https://stackoverflow.com/questions/48818985/variable-labels-in-the-r-package-haven-with-spss