Sandra Ross Sandra Ross - 2 months ago 6
R Question

Create the sequence 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4 with seq()

I'm trying to create the sequence

0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4
with the command
seq()
. Could anyone be able to tell me if it is possible to do such thing?

Answer

Yes, by wrapping seq() inside a call to rep() using its each argument:

> rep(seq(0,4), each=5)
[1] 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
>