我正在尝试从我加载 Haven 的 data.frame 中获取值标签列表。我的变量存储为haven_labelled,我知道值标签在那里,因为当我运行时,str()它们被列为属性。
str( x$tranwork )
'haven_labelled' num [1:498381] NA NA NA NA NA NA NA NA NA NA ...
- attr(*, "label")= chr "Means of transportation to work"
- attr(*, "format.stata")= chr "%24.0g"
- attr(*, "labels")= Named num [1:19] 0 10 11 12 13 14 15 20 30 31 ...
..- attr(*, "names")= chr [1:19] "N/A " "Auto, truck, or van" "Auto" "Driver" ...
>
Run Code Online (Sandbox Code Playgroud)
似乎有很多获得变量标签的好方法。我不知道如何使用SPSS 获取 R 包 Haven 中的值标签变量标签或使用 Haven 导入 Stata 数据后访问变量标签的便捷方法
我尝试过将变量转换为因子,并且
attr( x$tranwork , "label" )
[1] "Means of transportation to work"
> attr( x$tranwork , "names" )
NULL
Run Code Online (Sandbox Code Playgroud)
本质上我想看到与 x$transwork 1- 到 19 相关的标签
有几种方法可以获取值标签。
随labelled包:
library(labelled)
names(val_labels(x$tranwork))
Run Code Online (Sandbox Code Playgroud)
随sjlabelled包:
sjlabelled::get_labels(x$tranwork)
Run Code Online (Sandbox Code Playgroud)
和base:
names(attr(x$tranwork, "labels"))
Run Code Online (Sandbox Code Playgroud)
如果您想查看值标签和值,请使用:
labelled::val_labels(x$tranwork)
Run Code Online (Sandbox Code Playgroud)
或者
attr(x$tranwork, "labels")
Run Code Online (Sandbox Code Playgroud)