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, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----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.
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:> rows <- 1:3 > do.call(rbind, lapply(rows, rep, 5))[,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:> 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. >