An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101201/3edb15c7/attachment.pl>
Sequence for repeated numbers
5 messages · Luana Marotta, Jonathan P Daily, Jorge Ivan Velez +2 more
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(ID, grade) dat <- dat[order(grade),]
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
the thing itself have purpose? Or do we, what's the word... imbue it."
- Jubal Early, Firefly
r-help-bounces at r-project.org wrote on 12/01/2010 11:08:06 AM:
[image removed] [R] Sequence for repeated numbers Luana Marotta to: r-help, r-help-request 12/01/2010 11:09 AM Sent by: r-help-bounces at r-project.org 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101201/c91cc3cb/attachment.pl>
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 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu
On Wed, 1 Dec 2010, Luana Marotta wrote:
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.
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.