Q:

Is there a way to create this type of vector without using a for loop?

I have a vector with a bunch of numbers in it, lets say 3,2,0,0,0,1,2,....
I want to make a vector that has numbers based on the numbers in the above vector.
It's hard to explain, but the vector created from the above numbers would be 1,1,1,2,2,6,7,7
One appears three times because the number in the first spot is a three, two shows up twice because the second number is a two, and so on.
I can do this just fine with a for loop using rep(), but I would love a way to do this with sapply and a custom function (or an already existing one if there is such a thing). I'm not sure how to do it without a counter variable i.

A:

You can use rep in vectorized way here, looping over the position of each element with seq_along and repeating it x times. 
x <- c(3,2,0,0,0,1,2)

rep(seq_along(x), x)
#[1] 1 1 1 2 2 6 7 7

