m3<-m1[row.names(m1) %in% row.names(m2),]
red yellow blue green black
red 0.000000 2.236068 4.472136 6.708204 8.944272
blue 4.472136 2.236068 0.000000 2.236068 4.472136
green 6.708204 4.472136 2.236068 0.000000 2.236068
m3[,rownames(m3)]
red blue green
red 0.000000 4.472136 6.708204
blue 4.472136 0.000000 2.236068
green 6.708204 2.236068 0.000000
Subsetting matrices of different dimensions
-
30-06-2022 - |
Question
Let's say I have two similarity matrices of different dimensions and with some of their row.names
in common but not in the same order, such as:
> m1
red yellow blue green black
red 0.000000 2.236068 4.472136 6.708204 8.944272
yellow 2.236068 0.000000 2.236068 4.472136 6.708204
blue 4.472136 2.236068 0.000000 2.236068 4.472136
green 6.708204 4.472136 2.236068 0.000000 2.236068
black 8.944272 6.708204 4.472136 2.236068 0.000000
> m2
purple green blue red
purple 0.000000 0.081172 4.472136 6.708204
green 0.081172 0.000000 0.107647 4.472136
blue 4.472136 0.107647 0.000000 0.073217
red 6.708204 4.472136 0.073217 0.000000
I want to subset m1
to a new matrix that only contains the rows in common with m2
. The final result should look like:
> m3
red blue green
red 0.000000 4.472136 6.708204
blue 4.472136 0.000000 2.236068
green 6.708204 2.236068 0.000000
Note that in the "real" data, the matrices are hundreds of dimensions. The subset
command seems to be for subsetting data in reference to itself, not in reference to other data frames or matrices? Anyway I tried creating an index of the matches like so:
index <- m1 %in% m2
which is fine, but I get an error when trying to transform this object into a new matrix using cbind or a for loop. I know there must be a reasonably quick or elegant way to do this, but documentation seems a bit terse on this subject. Ideally after transforming m1
to m3
, I want to perform some basic arithmetic operations on the values in the matching elements of m2
and m3
such as m2(2,3) - m3(3,2) = -2.128421. Hope this makes sense.
Many thanks in advance!!
Solution
OTHER TIPS
Try something like merge(m1,m2,all.x=FALSE,all.y=TRUE,by="colorrow")
. First have to take the rownames of each matrix into a column named colorrow
.