[R] Number of replications of a term

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jan 24 19:29:26 CET 2006


Nice.  I timed it and its much faster than mine too.

On 1/24/06, Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> wrote:
> Laetitia Marisa wrote:
> > Hello,
> >
> > Is there a simple and fast function that returns a vector of the number
> > of replications for each object of a vector ?
> > For example :
> > I have a vector of IDs :
> > ids <- c( "ID1", "ID2", "ID2", "ID3", "ID3","ID3", "ID5")
> >
> >  I want the function returns the following vector where each term is the
> > number of replicates for the given id :
> > c( 1, 2, 2, 3,3,3,1 )
>
> One-liner:
>
>  > table(ids)[ids]
> ids
> ID1 ID2 ID2 ID3 ID3 ID3 ID5
>   1   2   2   3   3   3   1
>
>  'table(ids)' computes the counts, then the subscripting [ids] looks it
> all up.
>
>  Now try it on your 40,000-long vector!
>
> Barry
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list