如何在R中使用ff包合并两个大型data.frames?

时间:2013-07-10 17:51:22

标签: r join large-data

我有两个非常大的.csv文件,让我们称它们为CSV.1和CSV.2(CSV.1约为1.4 GB,CSV.2约为790 MB),我想加入它们公共字段“Id”上的完全外部联接。 CSV文件的字段有多种类型,一些是完全数字的,另一些是字符串。此外,CSV.1有大约190列和160万条记录,CSV.2有大约40列和570k记录。

最初,我编写并执行了以下代码:

first_csv <- read.csv("CSV.1")
second_csv <- read.csv("CSV.2")
joined_csv <- join(CSV.1, CSV.2, by="Id", type="full")

然而,这返回了典型的,你的RAM完全纳税,错误。所以我尝试了以下内容:

# Install and invoke the ff package
install.packages("ff")
library(ff)
library(plyr)

# Read in the data
first_csv <- read.csv("CSV.1")
second_csv <- read.csv("CSV.2")

# Convert dataframes to ffdf's, while freeing up memory
first_csv_ff <- as.ffdf(first_csv)
rm(first_csv)
gc()
second_csv_ff <- as.ffdf(second_csv)
rm(second_csv)
gc()

# Attempt to join the two ffdf's by "Id"
joined_csv <- join(first_csv_ff, second_csv_ff, by="Id", type="full")

R发出以下错误:

Error in as.hi.integer(x, maxindex = maxindex, dim = dim, vw = vw, pack = pack) : 
NAs in as.hi.integer

我也尝试过没有as.ffdf的“&lt; - ffdf()”,但也没有任何欢乐。

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以使用merge ff个套餐,仅供参考:

FULL Outer join ~ merge(x = df1, y = df2, ...., all = TRUE)

根据您的数据,这应该有效:

merge(first_csv_ff, second_csv_ff, by="Id", all=TRUE)

答案 1 :(得分:1)

ffbase包为ff包提供了基本的统计功能。

install.packages(ffbase)
require(ffbase)
#now perform the merge
merge(ffdf1,ffdf2,by="key")