Variable labels in the R package Haven with SPSS

老子叫甜甜 提交于 2020-01-23 05:20:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!