I have two symmetric matrices, but of different dimensions. The entries are identified by specific labels some of which are shared by both matrices. I would like to sum the two matrices, but retain the union of the two. In other words, I want the result to be the same size as the larger of the two matrices, but with the entries that they share added together. cbind() and rbind() don't work since the matrices are different sizes (symmetrically). Merge doesn't want to cooperate, although it might be because I can't understand the documentation. I tried the package "reshape" but didn't get very far with that either. I tried simply adding (+) them, but that was a stupid first try. Any help is appreciated! -- View this message in context: http://n4.nabble.com/Merging-Matrices-tp1605474p1605474.html Sent from the R help mailing list archive at Nabble.com.
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.
On Mar 19, 2010, at 4:11 PM, duncandonutz wrote:> > I have two symmetric matrices, but of different dimensions. The > entries are > identified by specific labels some of which are shared by both > matrices. I > would like to sum the two matrices, but retain the union of the > two. In > other words, I want the result to be the same size as the larger of > the two > matrices, but with the entries that they share added together. > > cbind() and rbind() don't work since the matrices are different sizes > (symmetrically). Merge doesn't want to cooperate, although it might > be > because I can't understand the documentation. I tried the package > "reshape" > but didn't get very far with that either. I tried simply adding (+) > them, > but that was a stupid first try.See if this effort to construct an example and implement what it sounds like you are asking is effective: > A <- matrix(1:25, 5) > B <- matrix(1:9, 3) > colnames(A) <- c(letters[1:5]) > colnames(B) <- c(letters[1:3]) > rownames(B) <- c(letters[1:3]) > rownames(A) <- c(letters[1:5]) > rownames(A)[1] <- "A" > intersect(rownames(A),rownames(B)) [1] "b" "c" > intersect(colnames(A),colnames(B)) [1] "a" "b" "c" > A[intersect(rownames(A),rownames(B)),intersect(colnames(A),colnames(B))] a b c b 2 7 12 c 3 8 13 > B[intersect(rownames(A),rownames(B)),intersect(colnames(A),colnames(B))] a b c b 2 5 8 c 3 6 9 > A [intersect (rownames(A),rownames(B)),intersect(colnames(A),colnames(B))] <- A [intersect (rownames(A),rownames(B)),intersect(colnames(A),colnames(B))] + B[intersect(rownames(A),rownames(B)),intersect(colnames(A),colnames(B))] > A a b c d e A 1 6 11 16 21 b 4 12 20 17 22 c 6 14 22 18 23 d 4 9 14 19 24 e 5 10 15 20 25 -- David> > Any help is appreciated! > -- > View this message in context: http://n4.nabble.com/Merging-Matrices-tp1605474p1605474.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.
________________________________________> From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of duncandonutz [dwadswor at unm.edu] > Sent: March 19, 2010 1:11 PM > To: r-help at r-project.org > Subject: [R] Merging Matrices > > I have two symmetric matrices, but of different dimensions. The entries are > identified by specific labels some of which are shared by both matrices. I > would like to sum the two matrices, but retain the union of the two. In > other words, I want the result to be the same size as the larger of the two > matrices, but with the entries that they share added together. > > cbind() and rbind() don't work since the matrices are different sizes > (symmetrically). Merge doesn't want to cooperate, although it might be > because I can't understand the documentation. I tried the package "reshape" > but didn't get very far with that either. I tried simply adding (+) them, > but that was a stupid first try.With appropriate indexing of the matrices via their shared names, it can be done as follows:> A <- matrix(1:9, nrow = 3) > A[lower.tri(A)] <- A[upper.tri(A)] > B <- matrix(1:25, nrow = 5) > B[lower.tri(B)] <- B[upper.tri(B)] > dimnames(A) <- list(letters[c(2, 4, 5)], letters[c(2, 4, 5)]) > dimnames(B) <- list(letters[1:5], letters[1:5]) > Ab d e b 1 4 7 d 4 5 8 e 7 8 9> Ba b c d e a 1 6 11 16 21 b 6 7 12 17 22 c 11 17 13 18 23 d 12 18 22 19 24 e 16 21 23 24 25> B[dimnames(A)[[1]], dimnames(A)[[1]]]b d e b 7 17 22 d 18 19 24 e 21 24 25> C <- B > C[dimnames(A)[[1]], dimnames(A)[[1]]] <- A + B[dimnames(A)[[1]], dimnames(A)[[1]]] > Ca b c d e a 1 6 11 16 21 b 6 8 12 21 29 c 11 17 13 18 23 d 12 22 22 24 32 e 16 28 23 32 34> C - Ba b c d e a 0 0 0 0 0 b 0 1 0 4 7 c 0 0 0 0 0 d 0 4 0 5 8 e 0 7 0 8 9>HTH Steve McKinney> Any help is appreciated! > -- > View this message in context: http://n4.nabble.com/Merging-Matrices-tp1605474p1605474.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.
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 instead. 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. >
Actually both the solutions I listed assume that every row in small is in big. Although that was the case in the example you displayed, If that is not the case in general then use intersect: ix <- intersect(rownames(big), rownames(small)) result <- big result[ix, ix] <- big[ix, ix] + small[ix, ix] On Sat, Mar 20, 2010 at 8:34 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> 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 > instead. > > 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. >> >