[R] replace id by running number

Phil Spector spector at stat.berkeley.edu
Sat Mar 27 17:10:56 CET 2010


The rle (run length encoding) function is ideal for
problems like this:

> y <- c(4,4,4,2,45,12,12)
> rr = rle(y)
> rep(seq(along=rr$values),rr$lengths)
[1] 1 1 1 2 3 4 4


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Sat, 27 Mar 2010, sun wrote:

> Dear all,
>
> I want to replace an (unsorted) id variable in a large dataset by a running
> number without changing the order of the cases.
>
> E.g.,
>
> y <- c(4,4,4,2,45,12,12)
>
> should be replaced by something like
>
> x <- c(1,1,1,2,3,4,4)
>
> Sorry for this simple question  & thank you very much for your help!
>
>
>
>
>
>
> 	[[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