Skip to content
Prev 95584 / 398500 Next

Comparing two matrices

On 7/6/2006 8:18 AM, Srinivas Iyyer wrote:
I think that works, but it's not as fast as some other ways of doing the 
same thing.  For example, table(tb) will give you a table of the counts 
of each pair of entries in tb.  pmin(table(tb), 1) will set the maximum 
count to 1.

An advantage of this approach is that it will show you if there are any 
entries in tb that aren't in your tmat (typos, etc.).  A disadvantage is 
that if there are any missing categories (e.g. G, Grape, Star in your 
sample) they won't show up at all, and you may need some manipulations 
to get things to look exactly the way you asked.  For example,

 > pmin(table(tb))
         V2
V1       A M O S
   Apple  1 0 1 1
   Mango  1 1 0 1
   Orange 1 0 1 1
 > pmin(table(tb[,2:1]))
    V1
V2  Apple Mango Orange
   A     1     1      1
   M     0     1      0
   O     1     0      1
   S     1     1      1


Duncan Murdoch