R包Haven中的变量标签,带SPSS

时间:2018-02-16 01:39:43

标签: r import r-haven

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

# 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)

2 个答案:

答案 0 :(得分:4)

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

lapply(with_haven, function(x) attributes(x)$label)

答案 1 :(得分:0)

该功能可以解决问题。

sapply(with_haven, attr,"label")