在stata和R中读取带有大变量的stata文件

时间:2013-01-18 13:49:41

标签: r memory stata

我正在尝试阅读statastata中包含超过5000个变量的大小为320 MB的R个文件。我首先使用stata来读取文件,但它可以读取的最大变量是5000.所以,我不能使用stata来读取stata文件。我的问题是:

  1. 有没有办法使用stata读取stata文件,首先要求只保留变量(我知道变量),以便变量数小于5000?

  2. 我是否可以在stata中阅读此R文件?我使用32位(Vista),R给我错误。 "Error: cannot allocate vector of size 21k.Kb"

  3. 我使用以下R代码来读取文件:

      #The stata file is in the webpage: http://www.federalreserve.gov/econresdata/scf/scf_2010survey.htm#STATADAT
    #1. set mem 400m
    set maxvar 4000
    use p10i6.dta, clear
    keep x8166 x8167 x8168 x8163 x8164 x2422 x2506 x2606 x2623 x604 x614 x623 x716 x507  x513 x526 x1706 x1705 x1806 x1805 x1906 x1905 x2002 x2012 x1409 x1509 x1609 x1415 x1515 x1615 x1417 x1517 x1617 x1619 x1621 x3124 x3224 x3324 x3129 x3229 x3329 x3335 x3408 x3412 x3416 x3420 x3424 x3428 x4020 x4024 x4028 x4018 x4022 x4026 x4030 x4022 x4026 x4030 x4018 x3507 x3511 x3515 x3519 x3523 x3527 x3506 x3510 x3514 x3518 x3522 x3526 x3529 x3804 x3807 x3810 x3813 x3816 x3818 x3930 x3721 x3821 x3823 x3825 x3827 x3829 x3822 x3824 x3826 x3828 x3830 
    save p10i6.dta, clear     
    
    
    #2. 
            library (foreign)
            year<-2010
            yr <- substr( year , 3 , 4 )
            p10i6.dta<-read.dta(paste0( "p" , yr , "i6.dta" ))
            saveRDS(p10i6.dta,file=paste0( "p" , yr , "i6.rda" ))
            p10i6.rda<-readRDS(paste0( "p" , yr , "i6.rda" ))
    

2 个答案:

答案 0 :(得分:4)

要将数据读入R,可能有办法使用memiscStata.file函数执行此操作。而不是读入所有变量,使用子集选择所需的变量。例如:

require(memisc)

?Stata.file
d1 <- subset(
        Stata.file(paste0( "p" , yr , "i6.dta" )),
        select=c(x8166, x2606, x2623, x604)
    )

答案 1 :(得分:3)

我认为你有StataIC或Stata的旧版本。当前的Stata / SE和Stata / MP可以读取多达32.000个变量。因此,第一个逻辑步骤是将Stata升级到可以处理更大数据集的版本。如果问题不是由于缺少可用内存,那就是......为此,Stata的错误消息会有所帮助。

正如Richard Herron在评论中所说,您应该能够使用以下方式读取数据的子集:

use X8166 X8167 ... using p10i6, clear

请记住,Stata区分大小写。根据您链接的网站,变量称为X...而不是x...

如果要使用foreign软件包将数据加载到R中,请确保将R可用的内存设置为R上可用的最大值。在Vista上将大约为3.5Gb:

memory.limit(3500)

如果这没有帮助,您的数据集将会太大,您可以使用Stata或R中的任何ASCII方法来加载网站上提供的ASCII数据。