在保留标签的同时使用R保存Stata文件

时间:2019-03-06 15:44:46

标签: r stata r-haven

我在R haven包中发现了一个错误,其中write_dta在Stata中读取时会丢弃一些标签。我在haven中有filed the bug,但是我确实需要找到解决方法,直到错误得以修复。下面是解释。

让我们定义一个带标签的double,其中仅一个带标签的NA值。

library(haven)
#> Warning: package 'haven' was built under R version 3.4.4

tst <-
  labelled(
    c(
      1:5,
      tagged_na("d")
    ),
    c(
      "Agree Strongly" = 1,
      "Agree" = 2,
      "Neither agree nor disagree" = 3,
      "Disagree" = 4,
      "Disagree strongly" = 5,
      "No answer" = tagged_na("d")
    )
  )

tst
#> <Labelled double>
#> [1]     1     2     3     4     5 NA(d)
#> 
#> Labels:
#>  value                      label
#>      1             Agree Strongly
#>      2                      Agree
#>      3 Neither agree nor disagree
#>      4                   Disagree
#>      5          Disagree strongly
#>  NA(d)                  No answer

write_dta(data.frame(freehms = tst), "test.dta", version = 13)

如果我将其加载到Stata中并输入tab freehms,则所有标签都是正确的:

screen shot 2019-02-23 at 4 06 36 pm

现在,如果我使用上面的代码并添加另一个标记的NA值,则write_dta出于某种原因会删除最后一个标签:

library(haven)
#> Warning: package 'haven' was built under R version 3.4.4

tst <-
  labelled(c(1:5,
             tagged_na('d'),
             ## Only added this
             tagged_na('c')
          ),
        c('Agree Strongly' = 1,
          'Agree' = 2,
          'Neither agree nor disagree' = 3,
          'Disagree' = 4,
          'Disagree strongly' = 5,
          'No answer' = tagged_na('d'),
            ## And this
          'Dont know' = tagged_na('c')
          )
        )

tst
#> <Labelled double>
#> [1]     1     2     3     4     5 NA(d) NA(c)
#> 
#> Labels:
#>  value                      label
#>      1             Agree Strongly
#>      2                      Agree
#>      3 Neither agree nor disagree
#>      4                   Disagree
#>      5          Disagree strongly
#>  NA(d)                  No answer
#>  NA(c)                  Dont know

write_dta(data.frame(freehms = tst), "test.dta", version = 13)

screen shot 2019-02-23 at 4 08 29 pm

我从write.dta开始研究了foreign函数,但是似乎在版本11之后不支持Stata版本。

1 个答案:

答案 0 :(得分:0)

尝试包括缺少的选项,以使缺少的值的摘要可视化,如图所示

tab freehms, mi