[R] problem with merging matrices

David Huffer David.Huffer at csosa.gov
Wed Jul 15 17:06:32 CEST 2009


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




More information about the R-help mailing list