rsequencerep

Generating an vector with rep and seq but without the c() function


Suppose that I am not allowed to use the c() function.

My target is to generate the vector

"1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9"

Here is my attempt:

rep(seq(1, 5, 1), 5)

# [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

rep(0:4,rep(5,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

So basically I am sum them up. But I wonder if there is a better way to use rep and seq functions ONLY.


Solution

  • Like so:

    1:5 + rep(0:4, each = 5)
    # [1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
    

    I like the sequence option as well:

    sequence(rep(5, 5), 1:5)