We can split "v1" use the grouping variable (created with %/% ) to form a list , and then combine ( c ) the corresponding 'v2' elements with list using Map and unlist .
unlist(Map(`c`, v2, split(v1, (seq_along(v1)-1)%/%3+1)), use.names=FALSE) #[1] "header1" "a1" "b1" "c1" "header2" "a2" "b2" #[8] "c2" "header3" "a3" "b3" "c3" "header4" "a4" #[15] "b4" "c4" "header5" "a5" "b5" "c5"
Or, if the length of "v1" is a multiple of "3", we can create a matrix with 'v1', cbind 'v2', transfer the output and convert matrix to vector with c .
c(t(cbind(v2,matrix(v1, ncol=3, byrow=TRUE))))
akrun
source share