[R] RE: Counting elements in 2 rows of a matrix

About this list Date view Thread view Subject view Author view Attachment view

From: Adaikalavan Ramasamy (ramasamy@stats.ox.ac.uk)
Date: Mon 02 Sep 2002 - 16:41:25 EST


Message-id: <003201c2524b$c2905960$461401a3@stats.ox.ac.uk>

Try either this or crossprod() ...
> x%*%t(x)

     [,1] [,2] [,3] [,4]
[1,] 2 0 2 2
[2,] 0 2 0 1
[3,] 2 0 2 2
[4,] 2 1 2 4

The above is "like" distance matrix. eg B and D have only one "1" in common.
Notice the symmetry about the diagonal.
The diagonals are not so useful as they tell you how many "1"'s A, B, C, D
have in them.
  ----- Original Message -----
  From: To: r-help@stat.math.ethz.ch
  Sent: Saturday, August 31, 2002 6:22 AM
  Subject: [R] Counting elements in 2 rows of a matrix

  Dear R-users:

  Sorry to bother so late with this question, which surely has simple
answer. I'm working with matrices that contain either "1" or "0", for
example:

  [,1] [,2] [,3] [,4] [,5]
  A 1 0 1 0 0
  B 0 1 0 1 0
  C 1 0 1 0 0
  D 1 1 1 0 1

  I want to count the number of "1" common to, say, A and B, the number of
"1" that appear only in A and the number of "1" that appear only in B.
Please let me know if there's a simple way of doing this without the
complicated for...next loops that have come across my mind.

  Fredy Mejía

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jun 2004 - 09:31:11 EST