[R] problem with merging matrices

David Huffer David.Huffer at csosa.gov
Wed Jul 15 21:43:22 CEST 2009


Jurgen,

I obviously didn't read the text of yours that I quoted (viz., "and that in case of a match between the row or column names the smallest value is retained"). My apologies.  

David 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David Huffer
Sent: Wednesday, July 15, 2009 11:07 AM
To: jurgen claesen; r-help at r-project.org
Subject: Re: [R] problem with merging matrices

On Wednesday, July 15, 2009 8:28 AM, jurgen claesen wrote:

 > ...I'm a relative new user of R and I have a
 > problem  with  merging   a   collection   of
 > matrices.  All  matrices  in this collection
 > have the same dimension (532  rows  and  532
 > columns),  but  can  differ  in  the row and
 > columns  names.  I'd  like  to  merge  these
 > matrices  in  such  a way that the resulting
 > matrix   only   contains   unique  row-  and
 > column-names and that in  case  of  a  match
 > between the row or column names the smallest
 > value is retained....
 >    As an example says more:
 >
 >    A1<-matrix(c(1:9), ncol=3, byrow=TRUE)
 >    rownames(A1)<-colnames(A1)<-c("a","b","c")
 >
 >      a b c
 >    a 1 2 3
 >    b 4 5 6
 >    c 7 8 9
 >
 >    A2<-matrix(c(7,1,3,10,2,7,3,1,8),ncol=3,byrow=TRUE)
 >    rownames(A2)<-colnames(A2)<-c("a","y","z")
 >
 >       a y z
 >    a  7 1 3
 >    y 10 2 7
 >    z  3 1 8
 >
 >    I want something like this to be returned:
 >
 >        a  b  c  y  z
 >    a   1  2  3  1  3
 >    b   4  5  6 NA NA
 >    c   7  8  9 NA NA
 >    y  10 NA NA  5  6
 >    z   3 NA NA  8  9
 >

Two questions (a) how do you want to decide which of the
elements gets dropped out---like the value 7 in A2 [1,1] above
and (b) what goes in the bottom corner of the new matrix? I
would have guessed i would have seen
   2 7
   1 8
in the corner.  Was that a typo or do you really want to
overwrite the values of the submatrix that *is* in A1?

david


--
 David
 
 -----------------------------------------------------
 David Huffer, Ph.D.               Senior Statistician
 CSOSA/Washington, DC           david.huffer at csosa.gov

______________________________________________
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