[R] Aggregate rows to see the number of occurences

Petr Pikal petr.pikal at precheza.cz
Mon Jun 7 13:27:22 CEST 2004


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]))

Cheers
Petr





On 7 Jun 2004 at 13:05, Petr Pikal wrote:

> Hallo
> 
> On 7 Jun 2004 at 12:18, Nicolas STRANSKY wrote:
> 
> > Hi,
> > 
> > I have a set of data like the following:
> >        [,1]  [,2]
> > [1,]   10    2
> > [2,]    7    0
> > [3,]    1    0
> > [4,]    1    0
> > [5,]   15    0
> > [6,]   17    4
> > [7,]    4    0
> > [8,]   19    8
> > [9,]   10    2
> > [10,]  19    5
> 
> Maybe it can be done more elegantly but table and match can 
> probably do what you want.
> 
> > tab
>    V2 V3
> 1  10  2
> 2   7  0
> 3   1  0
> 4   1  0
> 5  15  0
> 6  17  4
> 7   4  0
> 8  19  8
> 9  10  2
> 10 19  5
> 
> > counts<-table(tab[,1])
> > count.no<-as.numeric(names(counts))
> > selection<-match(tab[,1],count.no)
> 
> > cbind(tab,no=as.numeric(counts[selection]))
>    V2 V3 no
> 1  10  2  2
> 2   7  0  1
> 3   1  0  2
> 4   1  0  2
> 5  15  0  1
> 6  17  4  1
> 7   4  0  1
> 8  19  8  2
> 9  10  2  2
> 10 19  5  2
> >
> 
> Is this what you want?
> 
> Cheers
> Petr
> 
> > 
> > I'd like to aggregate it in order to obtain the frequency (the
> > number of occurences) for each couple of values (e.g.: (10,2)
> > appears twice, (7,0) appears once). Something cool would be to have
> > this value in a third column... I've been looking at aggregate() but
> > either I couldn't get the right parameters, or this is not the right
> > tool to use...
> > 
> > Thank's for any help !
> > 
> > -- 
> > 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
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> 
> Petr Pikal
> petr.pikal at precheza.cz
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list