I know that when I'm building up a data.table
row-by-row, it's best to pre-allocate space:
library(data.table)
dt <- data.table(x=rep(0,1000), y=rep(0,1000))
for(i in 1L:1000L) {
set(dt, i, 1L, runif(1))
set(dt, i, 2L, rnorm(1))
}
(In fact, if I don't pre-allocate, I get a segmentation fault with that code.)
If I don't know the number of rows in advance, then I need to grow dynamically, probably using exponential allocation or something. Will I need to manage that process myself, or is there any existing support in data.table
for dynamic growth?
Also, when I'm done appending rows, I'll probably have some allocated space left over, is there a truncate()
method or similar? Or should I just do dt <- dt[1:n,]
?