[R] Combine matrix

François Pinard pinard at iro.umontreal.ca
Thu Aug 16 15:48:34 CEST 2007


[Gianni Burgin]
>let say something like this

>>a=matrix(1:25, nrow=5)

>>rownames(a)=letters[1:5]
>> colnames(a)=rep("A", 5)

>> a
>  A  A  A  A  A
>a 1  6 11 16 21
>b 2  7 12 17 22
>c 3  8 13 18 23
>d 4  9 14 19 24
>e 5 10 15 20 25

>> b=matrix(1:40, nrow=8)
>> rownames(b)=c(rep("a",4),rep("b",4))
>> colnames(b)=rep("B", 5)

>> b
>  B  B  B  B  B
>a 1  9 17 25 33
>a 2 10 18 26 34
>a 3 11 19 27 35
>a 4 12 20 28 36
>b 5 13 21 29 37
>b 6 14 22 30 38
>b 7 15 23 31 39
>b 8 16 24 32 40

>as a results I wold like something like

>  A  A  A  A  A  B  B  B  B  B
>a 1  6 11 16 21  1  9 17 25 33
>a 1  6 11 16 21  2 10 18 26 34
>a 1  6 11 16 21  3 11 19 27 35
>a 1  6 11 16 21  4 12 20 28 36
>b 2  7 12 17 22  5 13 21 29 37
>b 2  7 12 17 22  6 14 22 30 38
>b 2  7 12 17 22  7 15 23 31 39
>b 2  7 12 17 22  8 16 24 32 40

>does it is clear? is there a function that automate this operation?

Like, maybe:

   cbind(a[rownames(b),], b)



-- 
François Pinard   http://pinard.progiciels-bpi.ca



More information about the R-help mailing list