Alex
Alex

Reputation: 19803

Merging multiple data.tables

I am aware that one can merge (join) two data.table with the merge function or the [.data.table function. However, if I have say 10, data.tables and want to use do.call to merge them all together, is there a function that would do this? Currently I resort to do.call(cbind, ...) which works for only very special cases.

Upvotes: 56

Views: 43165

Answers (4)

Shixiang Wang
Shixiang Wang

Reputation: 2371

To fresh answer from Michael Ohlrogge, here I create a function to do this task. You can inherit more arguments from merge.data.table()

mergeDTs <- function(dt_list, by = NULL, sort = FALSE) {
  Reduce(
    function(...) {
      merge(..., by = by, all = TRUE, sort = sort)
    }, dt_list)
}

Upvotes: 0

Michael Ohlrogge
Michael Ohlrogge

Reputation: 10980

To flesh out the solution suggested by @Henk in comments, here is how to use this to select the all = TRUE option in a concise formulation:

MergedDT = Reduce(function(...) merge(..., all = TRUE), List_of_DTs)

Upvotes: 42

HappyCoding
HappyCoding

Reputation: 5139

The latest version of data.table (1.10.5) has an easy syntax for table join, which can also be chained.

 set(DT1, key)
 set(DT2, key) 
  ...
 set(DT10, key)

 DT_cmb <- DT1[DT2,][DT3,]...[DT10,]

Upvotes: 8

Matt Dowle
Matt Dowle

Reputation: 59602

Not sure, but maybe (untested) :

Reduce(merge,list(DT1,DT2,DT3,...))

Upvotes: 71

Related Questions