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 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
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 5Maybe it can be done more elegantly but table and match can probably do what you want.> tabV2 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.htmlPetr Pikal petr.pikal at precheza.cz
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.htmlPetr Pikal petr.pikal at precheza.cz
Christophe Pallier
2004-Jun-07 11:30 UTC
[R] Aggregate rows to see the number of occurences
> I have a set of data like the following:> [,1] [,2] > [1,] 10 2 ... > [10,] 19 5 > > 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... >You can use: x=paste(a[,1],a[,2],sep=",") table(x) then, if you need to have the count for each line from the original table: table(x)[x] Or you could indeed use the 'aggregate' function: aggregate(a[,1],list(a[,1],a[,2]),length) This yields one line per unique value (but that may be what you want...) Christophe Pallier www.pallier.org