[R] binding rows from different matrices
Stephane DRAY
stephane.dray at umontreal.ca
Tue Jun 29 17:00:29 CEST 2004
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/
More information about the R-help
mailing list