我在尝试合并一个大文件和一个小文件时碰壁了。我有 read many关于 R 中内存管理的其他帖子,并且无法找到解决它的非极端(转到 64 位,上传到集群等)方法。我已经尝试了一些 bigmemory 包,但无法找到解决方案。在我沮丧地举起双手之前,我想我会在这里尝试。
我正在运行的代码如下所示:
#rm(list=ls())
localtempdir<- "F:/Temp/"
memory.limit(size=4095)
[1] 4095
memory.size(max=TRUE)
[1] 487.56
gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 170485 4.6 350000 9.4 350000 9.4
Vcells 102975 0.8 52633376 401.6 62529185 477.1
client_daily<-read.csv(paste(localtempdir,"client_daily.csv",sep=""),header=TRUE)
object.size(client_daily)
>130MB
sbp_demos<-read.csv(paste(localtempdir,"sbp_demos",sep=""))
object.size(demos)
>0.16MB
client_daily<-merge(client_daily,sbp_demos,by.x="OBID",by.y="OBID",all.x=TRUE)
Error: cannot allocate vector of size 5.0 MB
我想我在问有没有不涉及购买新硬件的聪明方法来解决这个问题?
merge
创建一个更大的对象。 我应该放弃吗? bigmemory 应该能够帮助解决这个问题吗?
非常感谢任何指导。
Details: R version 2.13.1 (2011-07-08) Platform: i386-pc-mingw32/i386 (32-bit) Intel 2 Duo Core @2.33GHz, 3.48GB RAM
最佳答案
正如 Chase 已经提到的,你可以试试 data.table或者 sqldf .
对于任何一种情况,如果您适本地设置索引,您可能会从中获得更多 yield 。
使用 data.table 你会:
dt1 <- data.table(sbp_demos, key='OBID')
dt2 <- data.table(client_daily, key='OBID')
## Do an INNER JOIN-like operation, where non-matching rows are removed
mi <- dt1[dt2, nomatch=0]
## Do a RIGHT JOIN(?)-like operation ... all rows in dt2 will be returned.
## If there is no matching row in dt1, the values in the dt1 columns for
## the merged row will be NA
mr <- dt1[dt2]
如果你去
sqldf
路线, look at example 4i on its website ...再次确保您正确使用索引。
关于r - 大合并/内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8595556/