[R] assign sequence numbers by group of value

Marc Schwartz MSchwartz at mn.rr.com
Sun Jun 11 13:15:32 CEST 2006


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 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.
> >



More information about the R-help mailing list