# [R] Merging Matrices

Gabor Grothendieck ggrothendieck at gmail.com
Sat Mar 20 13:34:49 CET 2010

```The original post said the matrices are symmetric but it seems from
the examples that they are not symmetric but do each have the same row
and column names so we have assumed only this latter condition

If your matrices are called big and small then:

ix <- rownames(big) %in% rownames(small)
result <- big
result[ix, ix] <- big[ix, ix] + small

Also in the example every row name in small is also a row name in big
so if that is true in general then the first line above could be just:

ix <- rownames(small)

In the future please display the output dput in your post to make it
easier to reproduce your data, e.g. dput(big)

On Fri, Mar 19, 2010 at 9:51 PM, duncandonutz <dwadswor at unm.edu> wrote:
>
> To clarify, the two matrices might look like this:
>
>     A  B  C  D  E  F
> A   1  2  3  4  5  6
> B   2  4  6  8  10 12
> C   0  0  0  0  0  0
> D   0  1  0  1  0  1
> E   3  6  9  11 13 15
> F   2  2  2  2  2  2
>
>     B  D  E
> B   4  9  13
> D   9  8  7
> E   1  0  1
>
> I would like this:
>
>     A  B  C  D  E  F
> A   1  2  3  4   5   6
> B   2  8  6  17 23 12
> C   0  0  0  0  0  0
> D   0 10  0  9  7  1
> E   3  7  9  11 14 15
> F   2  2  2  2  2  2
>
> Thanks.
> --
> View this message in context: http://n4.nabble.com/Merging-Matrices-tp1605474p1636943.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

```