[R] Help on comparing two matrices

David Winsemius dwinsemius at comcast.net
Sun Aug 23 22:36:35 CEST 2009


On Aug 23, 2009, at 4:14 PM, Michael Kogan wrote:

> Thanks for all the replies!
>
> Steve: I don't know whether my suggestion is a good one. I'm quite  
> new to programming, have absolutely no experience and this was the  
> only one I could think of. :-) I'm not sure whether I'm able to put  
> your tips into practice, unfortunately I had no time for much  
> reading today but I'll dive into it tomorrow.
>
> David: To be honest I don't understand your code yet, but the result  
> is not equivalent to the original matrix since the row sums don't  
> match, isn't it? Or is it intended like this? I'll try to ask Google  
> (or rather RSeek) tomorrow to understand your code. :-)

Not sure what you mean by the "row sums don't match.

All I did (working from the inside of that function outward) was:

a) concatenate the row values into a string:
 > Reduce(paste, sm[1,])
[1] "0 0 0 0 1 1 0 0"  # the innermost function applied to the first  
row.

b) do it for every row
 > sapply(1:7, function(x) Reduce(paste, sm[x,]))
[1] "0 0 0 0 1 1 0 0" "1 1 1 1 0 1 1 0" "1 1 1 1 0 0 1 0" "1 0 0 1 0 0  
0 0"
"0 0 1 1 1 0 0 1"
[6] "1 0 0 0 0 0 0 1" "1 1 0 0 1 1 0 1"

c) create a sorting vector from that vector (of characters):
 > order(sapply(1:7, function(x) Reduce(paste, sm[x,])) )
[1] 1 5 6 4 7 3 2

d) use that sort vector to order the rows:
 > sm[order(sapply(1:7, function(x) Reduce(paste, sm[x,])) ), ]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    0    1    1    0    0
[2,]    0    0    1    1    1    0    0    1
[3,]    1    0    0    0    0    0    0    1
[4,]    1    0    0    1    0    0    0    0
[5,]    1    1    0    0    1    1    0    1
[6,]    1    1    1    1    0    0    1    0
[7,]    1    1    1    1    0    1    1    0

All of the original vectors are output, just in a different order, so  
I am therefore confused.....  why you think the "rowSums don't  
match" .... don't match what?

I assumed you would take this process and apply it to _each_ of the  
two matrices in question and then see if you got a TRUE result with  
the identical function or perhaps the "==" function.

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list