【问题标题】:From Stata to R parsing issue从 Stata 到 R 解析问题
【发布时间】:2021-06-28 17:08:06
【问题描述】:

我在使用 Haven 包读取 R 中的 Stata 文件时遇到问题。我在这个板上看到了一些关于这个问题的讨论,但这里似乎没有答案。

download.file("http://economics.mit.edu/files/3828", "nswre74.dta")
download.file("http://economics.mit.edu/files/3824", "cps1re74.dta")
download.file("http://economics.mit.edu/files/3825", "cps3re74.dta")

nswre74  <- read_dta("nswre74.dta") 
cps1re74 <- read_dta("cps1re74.dta") 
cps3re74 <- read_dta("cps3re74.dta")

cps1re74 有效,但对于另外两个我得到以下错误:

nswre74

我尝试过使用外国包裹。我也尝试过使用 Latin1 或 UTF-8 进行编码,但似乎没有任何效果......

【问题讨论】:

  • 我可以看到第一个是 2007 年保存的非常小的数据集,因此无论大小还是日期都不应暗示任何问题。我不经常使用 R,所以无法评论 R 函数。
  • 查看 readstata13 包是否有效。
  • @G.Grothendieck No 似乎也不起作用。

标签: r stata


【解决方案1】:

使用 Haven (v. 2.4.1) 对我有用。 (R 版本 4.0.5)

library(haven) 
nswre74  <- read_dta("nswre74.dta") 
cps1re74 <- read_dta("cps1re74.dta") 
cps3re74 <- read_dta("cps3re74.dta")
> nswre74 %>% head(3)
# A tibble: 3 x 11
  treat   age    ed black  hisp married nodeg  re74  re75   re78  age2
  <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl>
1     1    37    11     1     0       1     1     0     0  9930.  1369
2     1    22     9     0     1       0     1     0     0  3596.   484
3     1    30    12     1     0       0     0     0     0 24909.   900
> cps1re74 %>% head(3)
# A tibble: 3 x 11
  treat   age    ed black  hisp married nodeg  re74  re75   re78  age2
  <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl>
1     1    37    11     1     0       1     1     0     0  9930.  1369
2     1    22     9     0     1       0     1     0     0  3596.   484
3     1    30    12     1     0       0     0     0     0 24909.   900
> cps3re74%>% head(3)
# A tibble: 3 x 11
  treat   age    ed black  hisp married nodeg  re74  re75   re78  age2
  <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl>
1     1    37    11     1     0       1     1     0     0  9930.  1369
2     1    22     9     0     1       0     1     0     0  3596.   484
3     1    30    12     1     0       0     0     0     0 24909.   900
> 

【讨论】:

  • 谢谢,由于某些原因,它仍然无法在我这边工作......
  • 你在用haven读取文件吗?哪个版本?你可以访问stata吗?您可以尝试在 stata 中打开文件并删除似乎太长的注释。或者将haven更新到最新版本,这对我来说很好。
  • 很遗憾我没有 Stata。我确实在使用来自 Haven 的 read_dta!
  • 哪个版本?我相信新版本会起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多