More than one value for “each” argument in “rep” function (R) ?

♀尐吖头ヾ 提交于 2019-11-27 08:42:15

问题


How to assign more than one value for "each" argument in "rep" function in R? A trivial example, where each value in a vector is 3-times repeated in a row:

a <- seq(2,6,2)
rep (a,each = 3)

However, if I add more than one value in "each" argument in order to change the number of repetition of each value, it doesn't work properly:

rep (a, each = c(2,4,7))

How to solve it? Thank you in advance.


回答1:


well, depending on what you think the output should be, I'm guessing you want the times= parameter

rep (a, times=c(2,4,7))
# [1] 2 2 4 4 4 4 6 6 6 6 6 6 6

See ?rep for the difference



来源:https://stackoverflow.com/questions/23818372/more-than-one-value-for-each-argument-in-rep-function-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!