我在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
,则所有标签都是正确的:
现在,如果我使用上面的代码并添加另一个标记的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)
我从write.dta
开始研究了foreign
函数,但是似乎在版本11之后不支持Stata版本。
答案 0 :(得分:0)
尝试包括缺少的选项,以使缺少的值的摘要可视化,如图所示
tab freehms, mi