How to roeder the rows of one matrix with respect to the other matrix?
Posted
by
user2806363
on Stack Overflow
See other posts from Stack Overflow
or by user2806363
Published on 2014-06-06T13:55:40Z
Indexed on
2014/06/06
15:25 UTC
Read the original article
Hit count: 214
r
I have two big matrices A and B with diffrent dimensions.I want to order the rows of matrix B with respect to rows of the matrix A. and add the rows with values 0 to matrix B, if that row is not exist in B but in A
Here is the reproduceable example and expected output:
A<-matrix(c(1:40), ncol=8)
rownames(A)<-c("B", "A", "C", "D", "E")
> A
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
B 1 6 11 16 21 26 31 36
A 2 7 12 17 22 27 32 37
C 3 8 13 18 23 28 33 38
D 4 9 14 19 24 29 34 39
E 5 10 15 20 25 30 35 40
> B<-matrix(c(100:108),ncol=3)
rownames(B)<-c("A", "E", "C")
> B
[,1] [,2] [,3]
A 100 103 106
E 101 104 107
C 102 105 108
Here is the Expected output :
>B
[,1] [,2] [,3]
B 0 0 0
A 100 103 106
C 102 105 108
D 0 0 0
E 101 104 107
>
Would someone help me to implement this in R ?
© Stack Overflow or respective owner