Re: [R] Sequence for repeated numbers

2010-12-01 Thread Greg Snow
Try this: id <- 1:20 grade <- c(4,4,4,5,5,7,7,7,7,8,8,8,9,9,9,9,9,10,10,10) sequence <- ave( id, grade, FUN=seq ) # if grade is not sorted grade2 <- sample(grade) sequence2 <- ave( id, grade2, FUN=seq ) cbind( grade2, sequence2 ) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermo

Re: [R] Sequence for repeated numbers

2010-12-01 Thread Phil Spector
Luana - It's probably not the most efficient way, but here's a solution that's not dependent on the grades being sorted: grade <- c(4,4,4,5,5,7,7,7,7,8,8,8,9,9,9,9,9,10,10,10) unlist(sapply(rle(grade)$lengths,function(x)seq(1,x))) [1] 1 2 3 1 2 1 2 3 4 1 2 3 1 2 3 4 5 1 2 3

Re: [R] Sequence for repeated numbers

2010-12-01 Thread Jorge Ivan Velez
Hi Luana, Try this: ID <- 1:20 grade <- c(4, 4, 4, 5, 5, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10) d <- data.frame(ID, grade) d$Sequence <- do.call(c, sapply(rle(grade)$lengths, seq)) d HTH, Jorge On Wed, Dec 1, 2010 at 11:08 AM, Luana Marotta <> wrote: > Hello fellows, > > I would like

Re: [R] Sequence for repeated numbers

2010-12-01 Thread Jonathan P Daily
Try this: > ord <- order(grade) > ID <- Id[ord] > grade <- grade[ord] > sequence <- unlist(sapply(table(grade), FUN = function(x) 1:x), use.names = F) And as a general tip, it is much easier to work with related values like ID and grade if they are in a data frame. Such as: > dat <- data.frame