我正在努力将标记数据的值和标签提取为R中的data.frame。在下面的示例中,我想提取值的两列数据框和标签
非常感谢您的时间和帮助。
download.file("http://mqasim.me/temp/Workingdata.RData", "Workingdata11.RData")
load("Workingdata11.RData")
library(labelled)
df <- wvs2i$V2
str(df)
Class 'labelled' atomic [1:24558] 32 32 32 32 32 32 32 32 32 32 ...
..- attr(*, "label")= chr "Country/region"
..- attr(*, "format.stata")= chr "%8.0g"
..- attr(*, "labels")= Named num [1:197] -5 -4 -3 -2 -1 4 8 12 16 20 ...
.. ..- attr(*, "names")= chr [1:197] "Missing; Unknown" "Not asked in survey" "Not applicable" "No answer" ...
head(df)
<Labelled double>
[1] 32 32 32 32 32 32
Labels:
value label
-5 Missing; Unknown
-4 Not asked in survey
-3 Not applicable
-2 No answer
-1 Don't know
4 Afghanistan
8 Albania
12 Algeria
16 American Samoa
20 Andorra
24 Angola
28 Antigua
31 Azerbaijan
32 Argentina
答案 0 :(得分:2)
label and values are stored as an attribute of df
. You can get what you want by:
stack(attr(df, 'labels'))
# values ind
# 1 -5 Missing; Unknown
# 2 -4 Not asked in survey
# 3 -3 Not applicable
# 4 -2 No answer
# 5 -1 Don't know
# 6 4 Afghanistan
# ......