We can do this by splitting. Create a function to create a grouping variable with gl
that increments at blocks of 'n' (here n is 4), then split both the vectors into a list
, use Map
to concatenate the corresponding list
elements and unlist
the list
to create a vector
f1 <- function(x, n) split(x, as.integer(gl(length(x), n, length(x))))
unlist( Map(c, f1(a, 4), f1(b, 4)), use.names = FALSE)
#[1] 723 680 2392 2063 1 2 3 4
#[9] 721 746 2053 2129 5 6 7 8
Or if the lengths are the same, then we can rbind
and concatenate after creating a matrix
c(rbind(matrix(a, nrow =4), matrix(b, nrow = 4)))
#[1] 723 680 2392 2063 1 2 3 4 721 746 2053 2129 5 6 7 8