Hi! Is there a fast way to duplicate rows in a matrix? I would like to do the following: change a matrix A like: [,1] [,2] [1,] Kevin 1 [2,] Alf 2 into : [,1] [,2] [1,] Kevin 1 [2,] Kevin 1 [3,] Alf 2 [4,] Alf 2 i.e. double all rows. The only way I could think off was: rbind(A[1,],A[1,],A[2,],A[2,]) - which is really impractible, of course. Can anyone help, please? Thank you, Silvia
My sugestion (for an alphabetically ordered output) AA <- rbind(A,A) AA <- AA[order(AA[,1]), ] ## so your matrix gets alphabetically ordered JM On Fri, 2007-11-02 at 09:04 -0700, Silvia Lipski wrote:> Hi! > > Is there a fast way to duplicate rows in a matrix? > I would like to do the following: > > change a matrix A like: > [,1] [,2] > [1,] Kevin 1 > [2,] Alf 2 > > into : > [,1] [,2] > [1,] Kevin 1 > [2,] Kevin 1 > [3,] Alf 2 > [4,] Alf 2 > > i.e. double all rows. The only way I could think off > was: rbind(A[1,],A[1,],A[2,],A[2,]) - which is really > impractible, of course. > > Can anyone help, please? > Thank you, > Silvia > > ______________________________________________ > 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.
On Fri, 2007-11-02 at 09:04 -0700, Silvia Lipski wrote:> Hi! > > Is there a fast way to duplicate rows in a matrix? > I would like to do the following: > > change a matrix A like: > [,1] [,2] > [1,] Kevin 1 > [2,] Alf 2 > > into : > [,1] [,2] > [1,] Kevin 1 > [2,] Kevin 1 > [3,] Alf 2 > [4,] Alf 2 > > i.e. double all rows. The only way I could think off > was: rbind(A[1,],A[1,],A[2,],A[2,]) - which is really > impractible, of course. > > Can anyone help, please? > Thank you, > SilviaTypically, you would use something like:> A[,1] [,2] [1,] "Kevin" "1" [2,] "Alf" "2"> A[rep(seq(nrow(A)), each = 2), ][,1] [,2] [1,] "Kevin" "1" [1,] "Kevin" "1" [2,] "Alf" "2" [2,] "Alf" "2" Use row indexing consisting of duplicate index values created by:> rep(seq(nrow(A)), each = 2)[1] 1 1 2 2 See ?rep and ?seq HTH, Marc Schwartz
... but you may not want the rows re-ordered. Here is a possible modification that uses a similar idea: m <- matrix(1:(2*nrow(A)), ncol = 2, byrow = TRUE) AA <- rbind(A, A)[order(m), ] Bill Venables -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Juan Manuel Barreneche Sent: Saturday, 3 November 2007 2:34 AM To: Silvia Lipski; r-help list Subject: Re: [R] duplicate rows in a matrix My sugestion (for an alphabetically ordered output) AA <- rbind(A,A) AA <- AA[order(AA[,1]), ] ## so your matrix gets alphabetically ordered JM On Fri, 2007-11-02 at 09:04 -0700, Silvia Lipski wrote:> Hi! > > Is there a fast way to duplicate rows in a matrix? > I would like to do the following: > > change a matrix A like: > [,1] [,2] > [1,] Kevin 1 > [2,] Alf 2 > > into : > [,1] [,2] > [1,] Kevin 1 > [2,] Kevin 1 > [3,] Alf 2 > [4,] Alf 2 > > i.e. double all rows. The only way I could think off > was: rbind(A[1,],A[1,],A[2,],A[2,]) - which is really > impractible, of course. > > Can anyone help, please? > Thank you, > Silvia > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.______________________________________________ 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.