Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using rep/seq to create ID column

Is there an efficient way to create an ID column using rep/seq or some other function I’m not thinking of to make a sequence such as the following:

1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10…..

So every 3 numbers the following 3 numbers get repeated an additional time. My actual data will require a sequence that is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1:1000- 1 each

1001:2000- 2 each

2001:3000 – 3 each
….

Any ideas/help would be greatly appreciated.

>Solution :

We can use

v2 <- 1:7000
rep(v2, as.integer(gl(length(v2), 1000, length(v2))))

For the first case

v1 <- 1:15
rep(v1, as.integer(gl(length(v1), 3, length(v1))))
 [1]  1  2  3  4  4  5  5  6  6  7  7  7  8  8  8  9  9  9 10 10 10 10 11 11 11 11 12 12 12 12 13 13 13 13 13 14 14 14 14 14 15 15 15 15
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading