导出带标签的双<dbl + lbl>数据类型

Lon*_*Rob 2 r tidyverse tibble

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

# 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
Run Code Online (Sandbox Code Playgroud)

如果我只想查看该SOC10M列,R报告该变量是a <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'
Run Code Online (Sandbox Code Playgroud)

我找不到有关此数据类型的任何文档。

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

这可能吗?

Eri*_*ail 5

我认为您可以找到此数据类型的文档,并使用避风港来桥接 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
Run Code Online (Sandbox Code Playgroud)