[R] assign sequence numbers by group of value

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 11 14:40:33 CEST 2006


This was recently discussed in:
 http://tolstoy.newcastle.edu.au/R/help/06/06/28664.html
Note that it depends on the groups elements being contiguous:

seq(id) - match(id, id) + 1

On 6/10/06, Tony Chu <gatony at gmail.com> wrote:
>  Dear R users:
>
>  I would like to assign sequence numbers based on group of value but
> cannot find an easy way.
> Here is a simple instance:
>
> > id = c('a','a','a','b','c','c')
> > id
> [1] "a" "a" "a" "b" "c" "c"
>
> I hope to create a corresponding vector as --
>
> [1]  1   2   3   1   1   2
>
> That is, in group "a"  the number begins with 1 and then continues to plus 1
> until
> it happens to the next group.  For "b" It goes back to 1.  Because there is
> only one
> b, it begins to be 1 again for the first c following, etc.  Could someone
> advise me
> how to do this in programming rather than manually?  Thanks a lot.
>
>  Tony C.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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