[R] RE: Counting elements in 2 rows of a matrix
Adaikalavan Ramasamy
ramasamy at stats.ox.ac.uk
Mon Sep 2 08:41:25 CEST 2002
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 at 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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list