Skip to content

growing dataframes with rbind

4 messages · Alexy Khrabrov, Greg Snow, Gabor Grothendieck +1 more

#
I'm growing a large dataframe by composing new rows and then doing

row <- compute.new.row.somehow(...)
d <- rbind(d,row)

Is this a fast/preferred way?
Cheers,
Alexy
#
If you know the final size that your matrix will be, it is better to preallocate the matrix, then insert the rows into the matrix:

mymat <- matrix( nrow=100, ncol=10 )
for( i in 1:100 ){
	mymat[i, ] <- rnorm(10)
}

Even better than this is to use replicate or sapply if you can, they will take all the rows created and construct the matrix for you.

The rbind way works, but it is slower and will fragment memory, so don't do that (and don't look at the reply in another thread where I just did :-).

Hope this helps,
#
Suppose we want 3 rows and the ith row should have 5 columns of i.
Create a list whose ith component is the ith row and rbind them:
[,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    2    2    2    2    2
[3,]    3    3    3    3    3
On Tue, Feb 24, 2009 at 1:01 PM, Alexy Khrabrov <deliverable at gmail.com> wrote: