In this question the data.table
package creator explains why rows cannot be inserted (or removed) by reference in the middle a data.table
yet. He also points out that such operations could be possible at end of the table. Could you show a code to perfome this action? It would be the "by reference" version of
a<- data.table(id=letters[1:2], var=1:2)
> a
id var
1: a 1
2: b 2
> rbind(a, data.table(id="c", var=3))
id var
1: a 1
2: b 2
3: c 3
thanks.
EDIT:
since a proper solution is not possible yet, which of the following is better (if internally different, not sure) either from a speed and memory usage perpective?
rbind(a, data.table(id="c", var=3))
rbindlist(list(a, data.table(id="c", var=3)))
are there eventually other (better) methods?
To answer your edit, just run a benchmark:
rbindlist
is clearly better thanrbind
. Thanks to Matthew Dowle pointing out the problems with using[
in a loop, I added another benchmark withset
.From the above your best options are using
rbindlist
, or sizing thedata.table
to begin with and then just populating the values (you can also use a similar strategy tostd::vector
inC++
, and double the size every time you run out of space, if you don't know the size of the data to begin with, and then once you're done filling it in, delete the extra rows).