gpt4 book ai didi

r - 导出带标签的双 数据类型

转载 作者:行者123 更新时间:2023-12-01 08:57:23 26 4
gpt4 key购买 nike

导入 SPSS 后 .sav文件,生成的 tibble 报告如下:

# A tibble: 88,528 x 7
CRY12 CRYOX7 INDS07M INECAC05 SOC10M URESMC GOR9D
<dbl+lbl> <dbl+lbl> <dbl+lbl> <dbl+lbl> <dbl+lbl> <dbl+lbl> <chr>
1 997 578 NA 31 NA 11 E12000009
2 921 926 NA 30 NA 11 E12000009
3 921 926 NA 31 NA 11 E12000009
4 372 372 NA 25 NA 11 E12000009
5 372 372 17 1 2211 11 E12000009
6 372 372 NA 34 NA 11 E12000009
7 921 926 18 2 3411 11 E12000009
8 921 926 NA 34 NA 11 E12000009
9 997 392 NA 25 NA 11 E12000009
10 997 392 3 1 2136 11 E12000009
# ... with 88,518 more rows

如果我只要求查看 SOC10M列,R 报告变量是 <Labelled double>并向我展示标签:

> df$SOC10M[1:10]
<Labelled double>
[1] NA NA NA NA 2211 NA 3411 NA NA 2136

Labels:
value label
-9 Does not apply
-8 No answer
1115 1115 'Chief executives and Snr officials'
1116 1116 'Elected officers and representatives'
1121 1121 'Production mngrs and directors in manufacturing'
1122 1122 'Production mngrs and directors in construction'
1123 1123 'Production mngrs and directors in mining and energy'

我找不到任何特定于此数据类型的文档。

我想将其导出到具有 label 的 csv每次,而不是 value . (即 CSV 应该在适当的情况下使用字符串而不是数字。)

这可能吗?

最佳答案

我认为您可以使用 haven 找到此数据类型的文档并桥接 SPSS-R gab .

根据我制作的这个示例的文档,我希望它是不言自明的。

# install.packages(c("haven"), dependencies = TRUE)
library(haven)
x1 <- labelled(c(1,NA, 5, 3, 5), c(Good = 1, Bad = 5) )
x2 <- labelled( c("M", "F", NA, "F", "M"), c(Male = "M", Female = "F") )

df <- tibble(x1, x2)
df
#> # A tibble: 5 x 2
#> x1 x2
#> <dbl+lbl> <chr+lbl>
#> 1 1 M
#> 2 NA F
#> 3 5 <NA>
#> 4 3 F
#> 5 5 M
#> >

# kinda like you are doing
df$x1[1:3]
#> <Labelled double>
#> [1] 1 NA 5
#>
#> Labels:
#> value label
#> 1 Good
#> 5 Bad

zap_labels(df$x1[1:3])
#> [1] 1 NA 5

as_factor(df$x2[1:3])
#> [1] Male Female <NA>
#> Levels: Female Male

zap_labels(df)
#> # A tibble: 5 x 2
#> x1 x2
#> <dbl> <chr>
#> 1 1 M
#> 2 NA F
#> 3 5 <NA>
#> 4 3 F
#> 5 5 M

关于r - 导出带标签的双 <dbl+lbl> 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45016787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com