Dear list I have a very simple question which is causing me problems! I have a matrix A and simply want to rbind this matrix together n times (n is a large number) How can I write this in R? I know I could do new<-rbind(z,z,z,...z) with z written n times but this will take forever as n is so large. Is there a simple way to write this? Cheers Mick -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hello Mick Try this:> a <- matrix(1:4,2,2) > a[,1] [,2] [1,] 1 3 [2,] 2 4> kronecker(rep(1,3),a)[,1] [,2] [1,] 1 3 [2,] 2 4 [3,] 1 3 [4,] 2 4 [5,] 1 3 [6,] 2 4>rksh> > Dear list > > I have a very simple question which is causing me problems! > > I have a matrix A and simply want to rbind this matrix together n times (n is a large number) > How can I write this in R? > I know I could do new<-rbind(z,z,z,...z) with z written n times but this will take forever as n is so large. > > Is there a simple way to write this? > > Cheers > Mick-- Robin Hankin, Lecturer, School of Geography and Environmental Science Tamaki Campus Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042 as of: Thu Nov 28 08:44:01 NZDT 2002 This (linux) system up continuously for: 455 days, 14 hours, 26 minutes -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
If you can somehow get all your matrices in a list, you can use do.call(). For example,> matlist <- list(a = matrix(0, 10, 4), b = matrix(1, 5, 4)) > do.call("rbind", matlist)[,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 [4,] 0 0 0 0 [5,] 0 0 0 0 [6,] 0 0 0 0 [7,] 0 0 0 0 [8,] 0 0 0 0 [9,] 0 0 0 0 [10,] 0 0 0 0 [11,] 1 1 1 1 [12,] 1 1 1 1 [13,] 1 1 1 1 [14,] 1 1 1 1 [15,] 1 1 1 1 -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Wed, 27 Nov 2002, Bayesianbay at aol.com wrote:> Dear list > > I have a very simple question which is causing me problems! > > I have a matrix A and simply want to rbind this matrix together n times (n is a large number) > How can I write this in R? > I know I could do new<-rbind(z,z,z,...z) with z written n times but this will take forever as n is so large. > > Is there a simple way to write this? > > Cheers > Mick > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 27 Nov 2002 Bayesianbay at aol.com wrote:> Dear list > > I have a very simple question which is causing me problems! > > I have a matrix A and simply want to rbind this matrix together n times (n is a large number) > How can I write this in R? > I know I could do new<-rbind(z,z,z,...z) with z written n times but this will take forever as n is so large. > > Is there a simple way to write this? >One way is t(matrix( rep(t(z), n), nrow=ncol(z))) There's probably a quicker way using aperm(), as well. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
At 01:23 PM 11/27/2002 -0500, Bayesianbay at aol.com wrote:>Dear list > >I have a very simple question which is causing me problems! > >I have a matrix A and simply want to rbind this matrix together n times (n >is a large number) >How can I write this in R? >I know I could do new<-rbind(z,z,z,...z) with z written n times but this >will take forever as n is so large. > >Is there a simple way to write this?An easy way is to not use rbind, but to use row-indexing, e.g.: > x <- matrix(1:4, ncol=2) > x [,1] [,2] [1,] 1 3 [2,] 2 4 > rep(seq(len=nrow(x)),3) [1] 1 2 1 2 1 2 > x[rep(seq(len=nrow(x)),3),] [,1] [,2] [1,] 1 3 [2,] 2 4 [3,] 1 3 [4,] 2 4 [5,] 1 3 [6,] 2 4 > -- Tony Plate -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
At 01:23 PM 11/27/2002 -0500, Bayesianbay at aol.com wrote:>I have a very simple question which is causing me problems! > >I have a matrix A and simply want to rbind this matrix together n times (n >is a large number) >How can I write this in R? >I know I could do new<-rbind(z,z,z,...z) with z written n times but this >will take forever as n is so large. > >Is there a simple way to write this?Dear Mick, If efficiency isn't an issue, you could do B <- A for (i in 1:(n - 1)) B <- rbind(B, A) A more efficient, but less transparent, approach would be matrix(rep(t(A), n), nrow(A)*n, ncol(A), byrow=TRUE) I hope that this helps, John ____________________________ John Fox Department of Sociology McMaster University email: jfox at mcmaster.ca web: http://www.socsci.mcmaster.ca/jfox ____________________________ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 27 Nov 2002 Bayesianbay at aol.com wrote:> Dear list > > I have a very simple question which is causing me problems! > > I have a matrix A and simply want to rbind this matrix together n times (n is a large number) > How can I write this in R? > I know I could do new<-rbind(z,z,z,...z) with z written n times but this will take forever as n is so large.Is z a matrix too? A more efficient way would be dn <- dim(z) new <- t(matrix(t(z), dn[2], dn[1]*n)) relying on recycling. As the t's show, it works most naturally for cbind. -- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._