data.table Reshaping, stacking and splitting Stacking multiple tables using rbindlist


Example

A common refrain in R goes along these lines:

You should not have a bunch of related tables with names like DT1, DT2, ..., DT11. Iteratively reading and assigning to objects by name is messy. The solution is a list of tables of data!

Such a list looks like

set.seed(1)
DT_list = lapply(setNames(1:3, paste0("D", 1:3)), function(i)
  data.table(id = 1:2, v = sample(letters, 2)))

$D1
   id v
1:  1 g
2:  2 j

$D2
   id v
1:  1 o
2:  2 w

$D3
   id v
1:  1 f
2:  2 w

Another perspective is that you should store these tables together as one table, by stacking them. This is straightforward to do using rbindlist:

DT = rbindlist(DT_list, id="src")

   src id v
1:  D1  1 g
2:  D1  2 j
3:  D2  1 o
4:  D2  2 w
5:  D3  1 f
6:  D3  2 w

This format makes a lot more sense with data.table syntax, where "by group" operations are common and straightforward.

For a deeper look, Gregor's answer might be a good place to start. Also check out ?rbindlist, of course. There's a separate example covering reading in a bunch of tables from CSV and then stacking them.