Skip to content

replacing random repeated numbers with a series of sequenced numbers

2 messages · york8866, Rui Barradas

#
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.
#
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: