Skip to content
Back to formatted view

Raw Message

Message-ID: <B37C0A15B8FB3C468B5BC7EBC7DA14CC63409F16F0@LP-EXMBVS10.CO.IHC.COM>
Date: 2010-12-01T17:07:01Z
From: Greg Snow
Subject: Sequence for repeated numbers
In-Reply-To: <AANLkTimVjzfdnA9fZaM9crnOZD=cKanTqzRLRAgf=KCn@mail.gmail.com>

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
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Luana Marotta
> Sent: Wednesday, December 01, 2010 9:08 AM
> To: r-help; r-help-request
> Subject: [R] Sequence for repeated numbers
> 
> Hello fellows,
> 
> I would like to create a sequence for repeated numbers in a dataset.
> For
> example:
> 
> ID <- c(1:20)
> grade <- c(4,4,4,5,5,7,7,7,7,8,8,8,9,9,9,9,9,10,10,10)
> 
> Data:
> 
> ID  Grade
> 1   4
> 2   4
> 3   4
> 4   5
> 5   5
> 6   7
> 7   7
> 8   7
> 9   7
> (...)
> 
> I would like to create a variable "sequence":
> 
> Data:
> ID Grade    Sequence:
> 1   4          1
> 2   4          2
> 3   4          3
> 4   5          1
> 5   5          2
> 6   7          1
> 7   7          2
> 8   7          3
> 9   7          4
> 
> Any help is very much appreciate!
> 
> Thank you,
> 
> Luana Marotta
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.