Re: [R] assign sequence numbers by group of value

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Sun 11 Jun 2006 - 21:15:32 EST

I might offer an additional possibility, in the case where your actual sequence might contain subsequent runs of the same values. This uses the rle() function to detect runs of a value in a vector.

For example:

> id2 <- c(id, id)
> id2

 [1] "a" "a" "a" "b" "c" "c" "a" "a" "a" "b" "c" "c"

> unlist(sapply(rle(id2)$lengths, seq))
 [1] 1 2 3 1 1 2 1 2 3 1 1 2

It also works for the case in your initial example:

> unlist(sapply(rle(id)$lengths, seq))

[1] 1 2 3 1 1 2

See ?rle for more information.

HTH, Marc Schwartz

On Sat, 2006-06-10 at 22:52 -0400, jim holtman wrote:
> > id
> [1] "a" "a" "a" "b" "c" "c"
> > x <- split(id, id) # separate by unique values
> > x <- lapply(x, seq) # generate the sequence numbers
> > x
> $a
> [1] 1 2 3
>
> $b
> [1] 1
>
> $c
> [1] 1 2
>
> > x <- unsplit(x, id) # make back into a vector
> > x
> [1] 1 2 3 1 1 2
> >
> >
>
>
> On 6/10/06, Tony Chu <gatony@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.
> >



R-help@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 Received on Sun Jun 11 21:21:33 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 11 Jun 2006 - 22:12:57 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.