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

标签 r tidyverse tibble

导入 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/

相关文章:

r - 使用 dplyr 获取术语列表、分组依据和汇总值

r - 在创建列表 tibble 列时在 "mutation"中使用 dplyr::sym() 会导致错误 is_symbol(x): object '.x' not found

python - 如何在 Python 和 R 之间交换 Msgpack 文件?

r - 在 dplyr 链中转换 Excel 日期格式

c++ - 如何包装第 3 方 DLL 以在 R 中使用?

r - 通过定界符将字符分隔成假定固定宽度的向量

javascript - 如何在 R 中的同一箱形图上绘制来自一个数据集的 2 组分类数据

r - 提取向量的一部分并以整齐的方式连接

r - 如何使用 Shiny 在工具提示中显示情节

r - 在 R 中按范围合并 - 应用循环