[R] replacing random repeated numbers with a series of sequenced numbers

Rui Barradas ruipbarradas at sapo.pt
Thu Oct 18 10:30:22 CEST 2012


Hello,

Try the following.

dat <- read.table(text="
ID
1001
1001
1001
1122
1122
1122
1421
1421
1789
1789
", header = TRUE)

r <- rle(dat$ID)
dat$SID <- rep(seq_along(r$lengths), r$lengths) + 1000

Hope this helps,

Rui Barradas
Em 18-10-2012 02:16, york8866 escreveu:
> Hi all,
>
> I have a dataset with one column like below:
> ID
> 1001
> 1001
> 1001
> 1122
> 1122
> 1122
> 1421
> 1421
> 1789
> 1789
> ..
>
> These numbers are no in sequence and they have different repeats.  How could
> replace them with sequenced numbers?
>
> Such as follows replacing the ID column with the SID column.
>
> ID	SID
> 1001	1001
> 1001	1001
> 1001	1001
> 1122	1002
> 1122	1002
> 1122	1002
> 1421	1003
> 1421	1003
> 1789	1004
> 1789	1004
>
>
> Thanks,
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/replacing-random-repeated-numbers-with-a-series-of-sequenced-numbers-tp4646559.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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