[R] growing dataframes with rbind

Greg Snow Greg.Snow at imail.org
Tue Feb 24 19:13:14 CET 2009

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,

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Alexy Khrabrov
> Sent: Tuesday, February 24, 2009 11:02 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] growing dataframes with rbind
> 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
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list