12

我正在尝试从带有 Have 包的 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)
4

2 回答 2

9

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

lapply(with_haven, function(x) attributes(x)$label)
于 2018-02-16T02:41:29.487 回答
0

该功能可以解决问题。

sapply(with_haven, attr,"label")

于 2020-03-24T06:22:10.547 回答