[R] Aggregate rows to see the number of occurences

Christophe Pallier pallier at lscp.ehess.fr
Mon Jun 7 13:30:44 CEST 2004


 > 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




More information about the R-help mailing list