有很多关于如何提取向量中每个第n个元素的问题,但我找不到如何在向量的每个第n个元素后面轻松添加一个值的方法。是否有一种简单的方法在向量的每个第n个元素后面添加某个特定的值?
举个例子,假设我们有两个向量:
v1 <- paste0(letters[1:3], rep(1:5, each = 3))
> v1
[1] "a1" "b1" "c1" "a2" "b2" "c2" "a3" "b3" "c3" "a4" "b4" "c4" "a5" "b5" "c5"
v2 <- paste0("header", seq(1:5))
> v2
[1] "header1" "header2" "header3" "header4" "header5"
现在我想在从第一个开始的每个v1
的第三个元素之后添加v2
的元素。结果应该像这样:
[1] "header1" "a1" "b1" "c1" "header2" "a2" "b2" "c2" "header3" "a3" "b3" "c3" "header4" "a4" "b4" "c4" "header5" "a5" "b5" "c5"