Q:

Insert elements into a vector at a given position, a given number of times

I have vector x <- c(1:50), and I want to insert y <- c(1,2) every 10nth position 5 times.
x <- c(1:50)
y <- c(1,2)
z <- c(seq(10,50,10))

The product has to be like this:
 1  2  3  4  5  6  7  8  9 10 1 2 11 12 13 14 15 16 17 18 19 20 1 2 21 22 23 24 25 26 27 28 29 30 1 2 31 32 33 34 35 36 37 38 39 40 1 2 41 42 43 44 45 46 47 48 49 50 1 2

I have tried with append, but it doesn't use "times"...

A:

We can split the 'x' into a list of vector based on a grouping index created with %/%``, concatenate (c) with the 'y' using 'Map and unlist the list to get the vector
unlist(Map(c, split(x, (x-1)%/%10), list(y)), use.names = FALSE)
#[1]  1  2  3  4  5  6  7  8  9 10  1  2 11 12 13 14 15 16 17 18 19 20  1  2 21
#[26] 22 23 24 25 26 27 28 29 30  1  2 31 32 33 34 35 36 37 38 39 40  1  2 41 42
#[51] 43 44 45 46 47 48 49 50  1  2

A:

You can use append if you use a loop ala How to insert elements into a vector?
x <- 1:50
y <- c(1,2)
z <- c(seq(10,50,10))

 for(i in 0:(length(z)-1)) {
   out <- append(x, y, after=(z[i+1]+i))
 }

 out

But I like akrun's suggestion

