york8866
2012-Oct-18 01:16 UTC
[R] replacing random repeated numbers with a series of sequenced numbers
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.
york8866
2012-Oct-18 02:18 UTC
[R] replacing random repeated numbers with a series of sequenced numbers
Hi arun, Thanks, I used your code, however, there's error message "Error: could not find function "count"". Thanks, -- View this message in context: http://r.789695.n4.nabble.com/replacing-random-repeated-numbers-with-a-series-of-sequenced-numbers-tp4646559p4646562.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2012-Oct-18 08:30 UTC
[R] replacing random repeated numbers with a series of sequenced numbers
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.
york8866
2012-Oct-18 12:58 UTC
[R] replacing random repeated numbers with a series of sequenced numbers
Hi Arun, Thanks, it works now. Hi Rui, Your code also works. Thanks, -- View this message in context: http://r.789695.n4.nabble.com/replacing-random-repeated-numbers-with-a-series-of-sequenced-numbers-tp4646559p4646609.html Sent from the R help mailing list archive at Nabble.com.