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]]
> 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. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What is the problem you are trying to solve? [[alternative HTML version deleted]]
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 >