[R] how to count "associated" factors?

zhihua li lzhtom at hotmail.com
Tue Jun 21 14:46:25 CEST 2005


hi netters

Suppose I have a factor X, with 10 elements and 3 levels: A B B C A C B A C 
C .

It is easy to count the number of elements for each level: 
tapply(X,X,length).

Now I have another factor Y, which formed a matrix with X:

 X| A B B C A C B A C C
 Y| B B C C C A A A B B

I wanna count the number of elements for each of these conditions: when X=A 
and Y=A; when X=A and Y=B; when X=A and Y=C; when X=B and Y=A; when X=B and 
Y=B; when X=B and Y=C; when X=C and Y=A; when X=C and Y=B; when X=C and 
Y=C.

The code I have written for this task is too complicated, involving a lot 
of for loops and if conditions. I believe there's some nice code that can 
do it far more efficiently. Can anyone give me a hint?

Thanks a lot!




More information about the R-help mailing list