[R] cross tabulation

James Reilly reilly at stat.auckland.ac.nz
Sun Oct 14 21:29:07 CEST 2007


On 14/10/07 5:39 PM, Louis Martin wrote:
> I am looking for an efficient and fast way for doing this:
> 
> for (v in 1:dw)      {ok <-list1[v,]
> 
>           for (z in 1:5)  {kk = ok[z] ; 
>           for (o in 1:t)  {if (kk== list[o]) n<- n+1}}
>           show(n)     }
> 
> list1  :  a data.frame with nrow = dw ; ncol = z;
> list2  :  a data.frame with nrow = 1   ; ncol = t
>  with t > z

Is the following close to what you want?

cumsum(apply(list1[,1:5], 1, function(x) sum(x %in% list2)))

Here list2 is a vector, not a data frame. Duplicates in list2 are not 
counted twice, unlike in your code, but you could run it again just on 
the duplicates (and again on the triplicates, etc) if this is needed.

Hope this helps,
James
-- 
James Reilly
Department of Statistics, University of Auckland
Private Bag 92019, Auckland, New Zealand



More information about the R-help mailing list