47
votes

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.

4

4 Answers

58
votes

Not sure, but maybe (untested) :

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

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)
7
votes

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,]
0
votes

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)
}