Re: [R] replace id by running number

2010-03-27 Thread Gabor Grothendieck
Here is a comparison of the speed of the solutions so far plus one based on cumsum. It seems that cumsum was the fastest and is 26x faster than the slowest solution. The speed may not be so important here and readability might be key in which case a good compromise might be match which was still p

Re: [R] replace id by running number

2010-03-27 Thread Dimitris Rizopoulos
try this: y <- c(4,4,4,2,45,12,12) match(y, unique(y)) I hope it helps. Best, Dimitris On 3/27/2010 4:42 PM, 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) sho

Re: [R] replace id by running number

2010-03-27 Thread Henrique Dallazuanna
Try this: as.numeric(factor(y, levels = unique(y))) On Sat, Mar 27, 2010 at 12:42 PM, 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 rep

Re: [R] replace id by running number

2010-03-27 Thread Phil Spector
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

[R] replace id by running number

2010-03-27 Thread sun
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!