[R] Slow computation in for loop

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed May 28 13:43:19 CEST 2003

On Wed, 28 May 2003, Philippe Grosjean wrote:

> I suspect that your problem comes from the rbind(). I have also noticed an
> exponentially slower execution with the increase of the size of the data
> frame that you rbind()s. It is much faster to rbind() several separated
> temporary data frames (let's say, ten by ten loops), and then to rbind()
> them all together.
> An even better solution is to allocate a data frame or matrix with the final
> size (it seems you can predict it in your example), and then use
> df[n, ] <- result
> where df is your allocated data frame and n is the iteration.

Since we are told the results are all numeric, it would be even better to 
use a pre-computed numeric matrix to store them.  rbind.data.frame is 
about the worst possible choice in such circumstances.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list