[R] Aggregate rows to see the number of occurences

Nicolas STRANSKY Nicolas.Stransky at curie.fr
Mon Jun 7 14:44:32 CEST 2004


Petr Pikal wrote:

> Oops, you wanted to count number of pairs, this modification 
> should work
> 
> int<-interaction(tab[,1],tab[,2])
> counts<-table(int)
> count.no<-names(counts)
> selection<-match(int,count.no)
> cbind(tab,no=as.numeric(counts[selection]))

Yes, thank's for all your answers. This is a way to do what I wanted to.

I have to also say that there is a "one-liner" to do it :) (hint by P. 
Hupé).

 > x <- cbind(c(10,7,1,1,15,17,4,19,10,19,10), c(2,0,0,0,0,4,0,8,2,5,2))
 > aggregate(x, list(a=x[,1],b=x[,2]),NROW)

-- 
Nicolas STRANSKY
Équipe Oncologie Moléculaire
Institut Curie - UMR 144 - CNRS                 Tel : +33 1 42 34 63 40
26, rue d'Ulm - 75248 Paris Cedex 5 - FRANCE    Fax : +33 1 42 34 63 49




More information about the R-help mailing list