[R] binding rows from different matrices

james.holtman@convergys.com james.holtman at convergys.com
Tue Jun 29 17:28:58 CEST 2004





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




More information about the R-help mailing list