[R] How to Merge based on Rows

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 8 23:34:39 CET 2010


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))
> out
  Apples 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.
>



More information about the R-help mailing list