Hello list, I have 3 matrices with same dimension : > veca=matrix(1:25,5,5) > vecb=matrix(letters[1:25],5,5) > vecc=matrix(LETTERS[1:25],5,5) I would like to obtain a new matrix composed by alternating rows of these different matrices (row 1 of mat 1, row 1 of mat 2, row 1 of mat 3, row 2 of mat 1.....) I have found a solution to do it but it is not very pretty and I wonder if I can do it in an other way (perhaps with apply ) ? > res=matrix(0,1,5) > for(i in 1:5) + res=rbind(res,veca[i,],vecb[i,],vecc[i,]) > res=res[-1,] > res [,1] [,2] [,3] [,4] [,5] [1,] "1" "6" "11" "16" "21" [2,] "a" "f" "k" "p" "u" [3,] "A" "F" "K" "P" "U" [4,] "2" "7" "12" "17" "22" [5,] "b" "g" "l" "q" "v" [6,] "B" "G" "L" "Q" "V" [7,] "3" "8" "13" "18" "23" [8,] "c" "h" "m" "r" "w" [9,] "C" "H" "M" "R" "W" [10,] "4" "9" "14" "19" "24" [11,] "d" "i" "n" "s" "x" [12,] "D" "I" "N" "S" "X" [13,] "5" "10" "15" "20" "25" [14,] "e" "j" "o" "t" "y" [15,] "E" "J" "O" "T" "Y" > Thanks in advance ! St??phane DRAY -------------------------------------------------------------------------------------------------- D??partement des Sciences Biologiques Universit?? de Montr??al, C.P. 6128, succursale centre-ville Montr??al, Qu??bec H3C 3J7, Canada Tel : 514 343 6111 poste 1233 E-mail : stephane.dray at umontreal.ca -------------------------------------------------------------------------------------------------- Web http://www.steph280.freesurf.fr/
james.holtman@convergys.com
2004-Jun-29 15:28 UTC
[R] binding rows from different matrices
Try:> veca=matrix(1:25,5,5) > vecb=matrix(letters[1:25],5,5) > vecc=matrix(LETTERS[1:25],5,5) > x.1 <- lapply(1:5,function(x)rbind(veca[x,],vecb[x,],vecc[x,])) > do.call('rbind',x.1)[,1] [,2] [,3] [,4] [,5] [1,] "1" "6" "11" "16" "21" [2,] "a" "f" "k" "p" "u" [3,] "A" "F" "K" "P" "U" [4,] "2" "7" "12" "17" "22" [5,] "b" "g" "l" "q" "v" [6,] "B" "G" "L" "Q" "V" [7,] "3" "8" "13" "18" "23" [8,] "c" "h" "m" "r" "w" [9,] "C" "H" "M" "R" "W" [10,] "4" "9" "14" "19" "24" [11,] "d" "i" "n" "s" "x" [12,] "D" "I" "N" "S" "X" [13,] "5" "10" "15" "20" "25" [14,] "e" "j" "o" "t" "y" [15,] "E" "J" "O" "T" "Y">__________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Office of Technology, Convergys james.holtman at convergys.com +1 (513) 723-2929 Stephane DRAY <stephane.dray at umontr To: r-help at stat.math.ethz.ch eal.ca> cc: Sent by: Subject: [R] binding rows from different matrices r-help-bounces at stat.m ath.ethz.ch 06/29/2004 11:00 Hello list, I have 3 matrices with same dimension : > veca=matrix(1:25,5,5) > vecb=matrix(letters[1:25],5,5) > vecc=matrix(LETTERS[1:25],5,5) I would like to obtain a new matrix composed by alternating rows of these different matrices (row 1 of mat 1, row 1 of mat 2, row 1 of mat 3, row 2 of mat 1.....) I have found a solution to do it but it is not very pretty and I wonder if I can do it in an other way (perhaps with apply ) ? > res=matrix(0,1,5) > for(i in 1:5) + res=rbind(res,veca[i,],vecb[i,],vecc[i,]) > res=res[-1,] > res [,1] [,2] [,3] [,4] [,5] [1,] "1" "6" "11" "16" "21" [2,] "a" "f" "k" "p" "u" [3,] "A" "F" "K" "P" "U" [4,] "2" "7" "12" "17" "22" [5,] "b" "g" "l" "q" "v" [6,] "B" "G" "L" "Q" "V" [7,] "3" "8" "13" "18" "23" [8,] "c" "h" "m" "r" "w" [9,] "C" "H" "M" "R" "W" [10,] "4" "9" "14" "19" "24" [11,] "d" "i" "n" "s" "x" [12,] "D" "I" "N" "S" "X" [13,] "5" "10" "15" "20" "25" [14,] "e" "j" "o" "t" "y" [15,] "E" "J" "O" "T" "Y" > Thanks in advance ! St??phane DRAY -------------------------------------------------------------------------------------------------- D??partement des Sciences Biologiques Universit?? de Montr??al, C.P. 6128, succursale centre-ville Montr??al, Qu??bec H3C 3J7, Canada Tel : 514 343 6111 poste 1233 E-mail : stephane.dray at umontreal.ca -------------------------------------------------------------------------------------------------- Web http://www.steph280.freesurf.fr/ ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
You can almost always index in such problems: here is one way. rbind(veca,vecb,vecc)[matrix(1:15, 3, byrow=T), ] Take it apart of see how it works, if it is not immediately obvious. On Tue, 29 Jun 2004, Stephane DRAY wrote:> Hello list, > I have 3 matrices with same dimension : > > veca=matrix(1:25,5,5) > > vecb=matrix(letters[1:25],5,5) > > vecc=matrix(LETTERS[1:25],5,5) > > I would like to obtain a new matrix composed by alternating rows of these > different matrices (row 1 of mat 1, row 1 of mat 2, row 1 of mat 3, row 2 > of mat 1.....) > > I have found a solution to do it but it is not very pretty and I wonder if > I can do it in an other way (perhaps with apply ) ? > > > res=matrix(0,1,5) > > for(i in 1:5) > + res=rbind(res,veca[i,],vecb[i,],vecc[i,]) > > res=res[-1,] > > res > [,1] [,2] [,3] [,4] [,5] > [1,] "1" "6" "11" "16" "21" > [2,] "a" "f" "k" "p" "u" > [3,] "A" "F" "K" "P" "U" > [4,] "2" "7" "12" "17" "22" > [5,] "b" "g" "l" "q" "v" > [6,] "B" "G" "L" "Q" "V" > [7,] "3" "8" "13" "18" "23" > [8,] "c" "h" "m" "r" "w" > [9,] "C" "H" "M" "R" "W" > [10,] "4" "9" "14" "19" "24" > [11,] "d" "i" "n" "s" "x" > [12,] "D" "I" "N" "S" "X" > [13,] "5" "10" "15" "20" "25" > [14,] "e" "j" "o" "t" "y" > [15,] "E" "J" "O" "T" "Y"-- 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