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:
1:1000- 1 each
1001:2000- 2 each
2001:3000 - 3 each ....
Any ideas/help would be greatly appreciated.
CodePudding user response:
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
CodePudding user response:
We can use rep
inside rep.int
.
First case:
rep.int(1:12, rep(1:4, each = 3))
Second case:
rep.int(1:3e3, rep(1:3, each = 1e3))