简体   繁体   English

合并更大数据的有效替代方案。框架R

[英]Efficient alternatives to merge for larger data.frames R

I am looking for an efficient (both computer resource wise and learning/implementation wise) method to merge two larger (size>1 million / 300 KB RData file) data frames. 我正在寻找一种有效的(计算机资源方面和学习/实现方式)方法来合并两个更大的(大小> 100万/ 300 KB RData文件)数据帧。

"merge" in base R and "join" in plyr appear to use up all my memory effectively crashing my system. 基础R中的“merge”和plyr中的“join”似乎耗尽了我的所有内存,有效地崩溃了我的系统。

Example
load test data frame 加载测试数据框

and try 并尝试

test.merged<-merge(test, test)

or 要么

test.merged<-join(test, test, type="all")  
    - -

The following post provides a list of merge and alternatives: 以下帖子提供了合并和备选方案的列表:
How to join (merge) data frames (inner, outer, left, right)? 如何加入(合并)数据框(内部,外部,左侧,右侧)?

The following allows object size inspection: 以下允许对象大小检查:
https://heuristically.wordpress.com/2010/01/04/r-memory-usage-statistics-variable/ https://heuristically.wordpress.com/2010/01/04/r-memory-usage-statistics-variable/

Data produced by anonym 匿名制作的数据

Here are some timings for the data.table vs. data.frame methods. 以下是data.table与data.frame方法的一些时序。
Using data.table is very much faster. 使用data.table非常快。 Regarding memory, I can informally report that the two methods are very similar (within 20%) in RAM use. 关于内存,我可以非正式地报告这两种方法在RAM使用方面非常相似(在20%以内)。

library(data.table)

set.seed(1234)
n = 1e6

data_frame_1 = data.frame(id=paste("id_", 1:n, sep=""),
                          factor1=sample(c("A", "B", "C"), n, replace=TRUE))
data_frame_2 = data.frame(id=sample(data_frame_1$id),
                          value1=rnorm(n))

data_table_1 = data.table(data_frame_1, key="id")
data_table_2 = data.table(data_frame_2, key="id")

system.time(df.merged <- merge(data_frame_1, data_frame_2))
#   user  system elapsed 
# 17.983   0.189  18.063 


system.time(dt.merged <- merge(data_table_1, data_table_2))
#   user  system elapsed 
#  0.729   0.099   0.821 

Here's the obligatory data.table example: 这是必须的data.table示例:

library(data.table)

## Fix up your example data.frame so that the columns aren't all factors
## (not necessary, but shows that data.table can now use numeric columns as keys)
cols <- c(1:5, 7:10)
test[cols] <- lapply(cols, FUN=function(X) as.numeric(as.character(test[[X]])))
test[11] <- as.logical(test[[11]])

## Create two data.tables with which to demonstrate a data.table merge
dt <- data.table(test, key=names(test))
dt2 <- copy(dt)
## Add to each one a unique non-keyed column
dt$X <- seq_len(nrow(dt))
dt2$Y <- rev(seq_len(nrow(dt)))

## Merge them based on the keyed columns (in both cases, all but the last) to ...
## (1) create a new data.table
dt3 <- dt[dt2]
## (2) or (poss. minimizing memory usage), just add column Y from dt2 to dt
dt[dt2,Y:=Y]

Do you have to do the merge in R? 你必须在R中合并吗? If not, merge the underlying data files using a simple file concatenation and then load them into R. (I realize this may not apply to your situation -- but if it does, it could save you a lot of headache.) 如果没有,使用简单的文件串联合并底层数据文件,然后将它们加载到R.(我意识到这可能不适用于您的情况 - 但如果确实如此,它可以为您节省很多麻烦。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM