Let's say that I have a bunch of matrices. They look like this (pardon using fruit for examples, my actual data tables are far too enormous): Matrix1 Apples Oranges Pears A 5 6 7 B 5 3 4 C 8 9 10 D 11 13 14 E 15 3 8 F 1 4 5 Matrix2 Apples Oranges Pears A 5 3 9 B 3 2 1 C 3 4 5 E 8 1 4 G 3 2 1 I cannot use cbind or rbind, because I have different rows in each matrix (e.g. - F & D are missing for Matrix2 and G is missing from Matrix1). What I would like to have in the end is this Apples Oranges Pears Apples Oranges Pears A 5 6 7 5 3 9 B 5 3 4 3 2 1 C 8 9 10 3 4 5 D 11 13 14 NA NA NA E 15 3 8 8 1 4 F 1 4 5 NA NA NA G NA NA NA 3 2 1 I have experimented with the merge() cmd, but my trial and error efforts have failed miserably. If anyone knows of a way to combine these matrices other than manually editing them together, please let me know. p.s. The way I have organized the matrices is such that they all have the same column names. So Matrix 1 and Matrix 2 will have the exact same column names - but I want the newly combined matrix to keep the columns separate. So, as in my final example, there would be one column titled "Apples" for each Matrix that I add in. -- View this message in context: http://n4.nabble.com/How-to-Merge-based-on-Rows-tp1010000p1010000.html Sent from the R help mailing list archive at Nabble.com.
Is this what you want:> x1$names <- rownames(x1) > x1Apples Oranges Pears names A 5 6 7 A B 5 3 4 B C 8 9 10 C D 11 13 14 D E 15 3 8 E F 1 4 5 F> x2$names <- rownames(x2) > x2Apples Oranges Pears names A 5 3 9 A B 3 2 1 B C 3 4 5 C E 8 1 4 E G 3 2 1 G> merge(x1, x2, by='names', all=TRUE)names Apples.x Oranges.x Pears.x Apples.y Oranges.y Pears.y 1 A 5 6 7 5 3 9 2 B 5 3 4 3 2 1 3 C 8 9 10 3 4 5 4 D 11 13 14 NA NA NA 5 E 15 3 8 8 1 4 6 F 1 4 5 NA NA NA 7 G NA NA NA 3 2 1>On Fri, Jan 8, 2010 at 4:21 PM, MRKidd <clistkoraelus@yahoo.com> wrote:> > Let's say that I have a bunch of matrices. > > They look like this (pardon using fruit for examples, my actual data tables > are far too enormous): > > Matrix1 > > Apples Oranges Pears > A 5 6 7 > B 5 3 4 > C 8 9 10 > D 11 13 14 > E 15 3 8 > F 1 4 5 > > > Matrix2 > > Apples Oranges Pears > A 5 3 9 > B 3 2 1 > C 3 4 5 > E 8 1 4 > G 3 2 1 > > > I cannot use cbind or rbind, because I have different rows in each matrix > (e.g. - F & D are missing for Matrix2 and G is missing from Matrix1). > > What I would like to have in the end is this > > Apples Oranges Pears Apples Oranges Pears > A 5 6 7 5 3 9 > B 5 3 4 3 2 1 > C 8 9 10 3 4 5 > D 11 13 14 NA NA NA > E 15 3 8 8 1 4 > F 1 4 5 NA NA NA > G NA NA NA 3 2 1 > > I have experimented with the merge() cmd, but my trial and error efforts > have failed miserably. If anyone knows of a way to combine these matrices > other than manually editing them together, please let me know. > > p.s. > The way I have organized the matrices is such that they all have the same > column names. So Matrix 1 and Matrix 2 will have the exact same column > names > - but I want the newly combined matrix to keep the columns separate. So, as > in my final example, there would be one column titled "Apples" for each > Matrix that I add in. > > > -- > View this message in context: > http://n4.nabble.com/How-to-Merge-based-on-Rows-tp1010000p1010000.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Try this. (The second line removes the suffixes in the column names.)> out <- merge(x1, x2, by = 0, all = TRUE)[-1] > names(out) <- sub("\\..*", "", names(out)) > outApples Oranges Pears Apples Oranges Pears 1 5 6 7 5 3 9 2 5 3 4 3 2 1 3 8 9 10 3 4 5 4 11 13 14 NA NA NA 5 15 3 8 8 1 4 6 1 4 5 NA NA NA 7 NA NA NA 3 2 1 On Fri, Jan 8, 2010 at 4:21 PM, MRKidd <clistkoraelus at yahoo.com> wrote:> > ?Let's say that I have a bunch of matrices. > > They look like this (pardon using fruit for examples, my actual data tables > are far too enormous): > > Matrix1 > > ? ? ?Apples ? Oranges ? Pears > A ? ? ? 5 ? ? ? ? ? 6 ? ? ? ? ? 7 > B ? ? ? 5 ? ? ? ? ? 3 ? ? ? ? ? 4 > C ? ? ? 8 ? ? ? ? ? 9 ? ? ? ? ? 10 > D ? ? ?11 ? ? ? ? ?13 ? ? ? ? ?14 > E ? ? ?15 ? ? ? ? ? ?3 ? ? ? ? ? 8 > F ? ? ? 1 ? ? ? ? ? ? 4 ? ? ? ? ? 5 > > > Matrix2 > > ? ? ?Apples ? ? Oranges ? Pears > A ? ? ? 5 ? ? ? ? ? ? 3 ? ? ? ? ? 9 > B ? ? ? 3 ? ? ? ? ? ? 2 ? ? ? ? ? ?1 > C ? ? ? 3 ? ? ? ? ? ? 4 ? ? ? ? ? ?5 > E ? ? ? 8 ? ? ? ? ? ? 1 ? ? ? ? ? ?4 > G ? ? ? 3 ? ? ? ? ? ?2 ? ? ? ? ? ? 1 > > > I cannot use cbind or rbind, because I have different rows in each matrix > (e.g. - F & D are missing for Matrix2 and G is missing from Matrix1). > > What I would like to have in the end is this > > ? ? ?Apples ? ?Oranges ? ?Pears ? ? Apples ? Oranges ? Pears > A ? ? ?5 ? ? ? ? ? ? 6 ? ? ? ? ? ?7 ? ? ? ? ? ?5 ? ? ? ? ? 3 ? ? ? ? ? 9 > B ? ? ?5 ? ? ? ? ? ? 3 ? ? ? ? ? ?4 ? ? ? ? ? ?3 ? ? ? ? ? 2 ? ? ? ? ? ?1 > C ? ? ?8 ? ? ? ? ? ? 9 ? ? ? ? ? ?10 ? ? ? ? ?3 ? ? ? ? ? ?4 ? ? ? ? ? 5 > D ? ? ?11 ? ? ? ? ? 13 ? ? ? ? ? 14 ? ? ? ? NA ? ? ? ? ?NA ? ? ? ? NA > E ? ? ? 15 ? ? ? ? ?3 ? ? ? ? ? ? ?8 ? ? ? ? ? 8 ? ? ? ? ? 1 ? ? ? ? ? ?4 > F ? ? ? 1 ? ? ? ? ? ?4 ? ? ? ? ? ? 5 ? ? ? ? ? NA ? ? ? ? NA ? ? ? ? NA > G ? ? ?NA ? ? ? ? ?NA ? ? ? ? ?NA ? ? ? ? ?3 ? ? ? ? ? ?2 ? ? ? ? ? 1 > > I have experimented with the merge() cmd, but my trial and error efforts > have failed miserably. If anyone knows of a way to combine these matrices > other than manually editing them together, please let me know. > > p.s. > The way I have organized the matrices is such that they all have the same > column names. So Matrix 1 and Matrix 2 will have the exact same column names > - but I want the newly combined matrix to keep the columns separate. So, as > in my final example, there would be one column titled "Apples" for each > Matrix that I add in. > > > -- > View this message in context: http://n4.nabble.com/How-to-Merge-based-on-Rows-tp1010000p1010000.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. >
Maybe Matching Threads
- Problem with lsa package (data.frame) on Windows XP
- Read data from .csv file as a matrix and compare the different between two matrix
- rownames cannot allocate vector of size
- Reg : using two different matrix : how to do t.test
- function in R that's equivalent to SQL's "IN"