[R] Sequence for repeated numbers
Phil Spector
spector at stat.berkeley.edu
Wed Dec 1 17:47:14 CET 2010
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.
>
More information about the R-help
mailing list