How to generate a series (1,2,2,3,3,3,4,4,4,4)

r, series

Solution

rep(1:4, 1:4)
[1] 1 2 2 3 3 3 4 4 4 4

Similarly for another series we get

rep(1:5, 1:5) * (-1)^rep(0:4, 1:5)
[1]  1 -2 -2  3  3  3 -4 -4 -4 -4  5  5  5  5  5

Problem

I am confused that how to generate a series (like this `1,2,2,3,3,3,4,4,4,4`) in R. I use the `for loop` and `rep` function like this, ``` for (i in 1:10) {rep(i,i)} ``` but I can't paste them together.

Original source