6

我试图合并一个大文件和一个小文件时碰壁了。我已经阅读 了许多其他关于 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

我想我在问有没有不涉及购买新硬件的聪明方法?

  1. 我需要能够merge创建一个更大的对象。
  2. 然后我需要对那个更大的对象进行回归等。

我应该放弃吗?bigmemory 应该能够帮助解决这个问题吗?

非常感谢任何指导。

详细信息:R 版本 2.13.1 (2011-07-08) 平台:i386-pc-mingw32/i386 (32-bit) Intel 2 Duo Core @2.33GHz, 3.48GB RAM

4

1 回答 1

8

正如 Chase 已经提到的,您可以尝试data.table或者sqldf

对于任何一个,如果您适当地设置索引,您可能会从中获得更多收益。

使用 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条路,请查看其网站上的示例 4i ...再次,确保您正确使用索引。

于 2011-12-21T20:34:17.407 回答