简体   繁体   English

将元素插入到给定位置的向量中,给定次数

[英]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) ,我想每隔10个位置插入y <- c(1,2) 5次。

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"... 我尝试过追加,但它不使用“时代”......

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 我们可以split的“X”到listvector基础上创建一组指数%/%``, concatenate ( Ç ) with the 'y' using 'Mapunlistlist ,以获得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

You can use append if you use a loop ala How to insert elements into a vector? 如果使用循环ala,可以使用append 如何将元素插入向量?

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 但我喜欢阿克伦的建议

A simple solution is obtained by transforming x into a 10X5 matrix and then incrementally rbind ing 1 and 2. c is used to return a vector. 一个简单的解决方案是通过将x转换成一个10X5矩阵,然后逐步得到rbind荷兰国际集团1和2 c用于返回的载体。

c(rbind(rbind(matrix(x, 10), 1), 2))
 [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 22 23 24
[29] 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
[57] 49 50  1  2

This can be generalize to more than 2 values by putting them in a list and using Reduce : 通过将它们放入列表并使用Reduce可以将其推广到2个以上的值:

c(Reduce(rbind, list(matrix(x, 10), 1, 2)))

Following @akrun's comment, you can use y in place a 1,2 in the line above with 按照@ akrun的评论,您可以在上面的行中使用y代替1,2

c(Reduce(rbind, append(list(matrix(x, 10)), y)))

or 要么

c(Reduce(rbind, c(list(matrix(x, 10)), as.list(y))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 写一个 function 在向量中给定的 position 处插入一个值 - Write a function to insert a value at a given position in a vector 根据给定的值和它们出现的次数在 R 中创建一个向量 - Create a vector in R from the given values and the number of times they present 替换给定 R 中另一个向量的负数位置的列? - replace a column given the position of a negative number of another vector in R? 创建给定数字的二进制向量 - Create binary vector of a given number 通过给定的序列/语句增加向量中元素的值 - Increasing the value of elements in a vector by a given sequence/statement 如何根据给定的索引对向量中的元素重新排序 - How to reorder the elements in the vector according to the indexes given 如何在数据框中插入新行,使其与R中给定向量的长度相等 - How to insert new row in a dataframe to make it equal row number as the length of given vector in R 在 R 中重复特定行给定的次数 - Repeat specific rows a given number of times in R R-如何在R中的命名向量中获取/计算属于给定名称的元素的数量 - R - How to get / count number of elements belonging to a given name in a named vector in R 更改arma :: vec中给定位置的元素,并更改其他向量中的相应元素 - Change elements at given positions in an arma::vec with corresponding elements in other vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM