[R] Counting two factors at the same time

Fabrice DELENTE fdelente at mail.cpod.fr
Fri Jan 22 19:51:51 CET 2010


Hello.

I'm trying to count string data that correspond to a given
condition in two factors of the same length.

For example, I have one factor

[ 'A', 'B', 'A', 'C', 'B', 'D', 'B' ]

and another is 

[ '1', '2', '2', '3', '2', '2', '3' ]

I'd like to count the occurences of 'B' and '2' (so in my example, I should
get 2 as an answer, since there are 2 'B' that correspond to '2' in the
other factor).

I tried doing it with loops (looping on one factor for 'B', looking for
the corresponding value in the second factor, and incrementing a counter
when the corresponding value is '2'), but I didn't succeed, and I know it's
not the R-way.

Any hint or direction?

Thanks.

-- 
Fabrice DELENTE



More information about the R-help mailing list