Hi all, I have a matrix, for example [,1] [,2] [1,] 1 3 [2,] 4 6 I want to replicate the matrix twice and add an extra column at the end, which is [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 3 1 3 1 3 2 [2,] 4 6 4 6 4 6 5 I found 'rep' only works for vector. Does anyone know how to replicate a matrix, and append the matrix? Thank you in advance, Wendy -- View this message in context: http://n4.nabble.com/replicate-matrix-tp1563337p1563337.html Sent from the R help mailing list archive at Nabble.com.
Hello Wendy, The simplest solution that comes to mind is to just use cbind. For instance:> x <- matrix(1:4, nrow=2)> x[,1] [,2] [1,] 1 3 [2,] 2 4> cbind(x,x,x,c(2,5)) # repeat x here as many times as needed[,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 3 1 3 1 3 2 [2,] 2 4 2 4 2 4 5 I hope that helps. Admittedly, this is not the best solution if you need to replicate a matrix many times. Best, Joshua On Sat, Feb 20, 2010 at 6:58 PM, wendy <wendy.qiao@utoronto.ca> wrote:> > Hi all, > > I have a matrix, for example > [,1] [,2] > [1,] 1 3 > [2,] 4 6 > > I want to replicate the matrix twice and add an extra column at the end, > which is > [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,] 1 3 1 3 1 3 2 > [2,] 4 6 4 6 4 6 5 > > I found 'rep' only works for vector. Does anyone know how to replicate a > matrix, and append the matrix? > > Thank you in advance, > Wendy > > > -- > View this message in context: > http://n4.nabble.com/replicate-matrix-tp1563337p1563337.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ [[alternative HTML version deleted]]
try this: mat <- matrix(c(1,4,3,6), 2, 2) cbind( do.call(cbind, rep(list(mat), 3)), c(2, 5) ) I hope it helps. Best, Dimitris wendy wrote:> Hi all, > > I have a matrix, for example > [,1] [,2] > [1,] 1 3 > [2,] 4 6 > > I want to replicate the matrix twice and add an extra column at the end, > which is > [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,] 1 3 1 3 1 3 2 > [2,] 4 6 4 6 4 6 5 > > I found 'rep' only works for vector. Does anyone know how to replicate a > matrix, and append the matrix? > > Thank you in advance, > Wendy > >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
You can just rep() it, and c() with extra data, and then matrix() it again:> m <- matrix(c(1,4,3,6),2) > matrix(c(rep(m, 3), c(2, 5)), nrow(m))[,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 3 1 3 1 3 2 [2,] 4 6 4 6 4 6 5 On Sun, Feb 21, 2010 at 10:58 AM, wendy <wendy.qiao at utoronto.ca> wrote:> > Hi all, > > I have a matrix, for example > ? ? [,1] [,2] > [1,] ?1 ? ?3 > [2,] ?4 ? ?6 > > I want to replicate the matrix twice and add an extra column at the end, > which is > ? ? [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,] ?1 ? ?3 ? ? 1 ? ?3 ? ? 1 ? ?3 ? ?2 > [2,] ?4 ? ?6 ? ? 4 ? ?6 ? ? 4 ? ?6 ? ?5 > > I found 'rep' only works for vector. Does anyone know how to replicate a > matrix, and append the matrix? > > Thank you in advance, > Wendy > > > -- > View this message in context: http://n4.nabble.com/replicate-matrix-tp1563337p1563337.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of wendy > Sent: Saturday, February 20, 2010 6:58 PM > To: r-help at r-project.org > Subject: [R] replicate matrix > > > Hi all, > > I have a matrix, for example > [,1] [,2] > [1,] 1 3 > [2,] 4 6 > > I want to replicate the matrix twice and add an extra column > at the end, > which is > [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,] 1 3 1 3 1 3 2 > [2,] 4 6 4 6 4 6 5 > > I found 'rep' only works for vector. Does anyone know how to > replicate a > matrix, and append the matrix?You want to replicate the columns of the matrix so use rep() in a column subscripting expression: x <- matrix(c(1,4,3,6),nrow=2,ncol=2) # setup xRepped <- x[ , rep(seq_len(ncol(x)), 3)] then append the new column with xReppedAppended <- cbind(xRepped, c(2,5)) I avoid the solutions that convert the original matrix to a vector and back to a matrix, like xxx <- matrix(rep(as.vector(x),3), nrow=2) as they make me assume more than I want to assume about how matrices are represented internally. Those solutions also lose any column or row names that my matrix may have had. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > Thank you in advance, > Wendy > > > -- > View this message in context: > http://n4.nabble.com/replicate-matrix-tp1563337p1563337.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >