R包Haven中带有SPSS的可变标签 [英] Variable labels in the R package Haven with SPSS

查看:135
本文介绍了R包Haven中带有SPSS的可变标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用避风港软件包从SPSS por文件访问变量标签(这是变量的描述).我可以用外国包裹做得很好,但我想使用避风港.有什么建议吗?

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)

推荐答案

read_spss中所述,标签存储为每一列的属性,而不是data.frame的属性.试试

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)

这篇关于R包Haven中带有SPSS的可变标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆