从使用 Haven 加载的 Stata 文件中提取值标签(值标签不是变量标签)
Extract Value Labels from a Stata file loaded with Haven (Value Labels not Variable Labels)
我正在尝试从 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" ...
>
似乎有很多获取变量标签的好方法。我不知道如何获取值标签 or Convenient way to access variables label after importing Stata data with haven
我试过将变量转换为因子,
attr( x$tranwork , "label" )
[1] "Means of transportation to work"
> attr( x$tranwork , "names" )
NULL
基本上我希望看到与 x$transwork 1- 到 19 关联的标签
有几种方法可以获取值标签。
使用 labelled
包:
library(labelled)
names(val_labels(x$tranwork))
使用 sjlabelled
包:
sjlabelled::get_labels(x$tranwork)
与base
:
names(attr(x$tranwork, "labels"))
如果您想查看值标签和值,请使用:
labelled::val_labels(x$tranwork)
或
attr(x$tranwork, "labels")
我正在尝试从 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" ...
>
似乎有很多获取变量标签的好方法。我不知道如何获取值标签
我试过将变量转换为因子,
attr( x$tranwork , "label" )
[1] "Means of transportation to work"
> attr( x$tranwork , "names" )
NULL
基本上我希望看到与 x$transwork 1- 到 19 关联的标签
有几种方法可以获取值标签。
使用 labelled
包:
library(labelled)
names(val_labels(x$tranwork))
使用 sjlabelled
包:
sjlabelled::get_labels(x$tranwork)
与base
:
names(attr(x$tranwork, "labels"))
如果您想查看值标签和值,请使用:
labelled::val_labels(x$tranwork)
或
attr(x$tranwork, "labels")