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)
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)
该功能可以解决问题。
sapply(with_haven, attr,"label")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.