[R] Counting two factors at the same time

Henrique Dallazuanna wwwhsd at gmail.com
Fri Jan 22 19:55:00 CET 2010


Try this;

> f <- c( 'A', 'B', 'A', 'C', 'B', 'D', 'B')
> n <- c('1', '2', '2', '3', '2', '2', '3')
> table(paste(f, n))

On Fri, Jan 22, 2010 at 4:51 PM, Fabrice DELENTE <fdelente at mail.cpod.fr> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list