生成一个向量,其中 x、y、z 重复 n 次,并具有固定的上限
我正在尝试创建一个矢量,其中我有3个重复的数字1,然后是数字2的3个重复,例如,数字36的3个重复。
c(1,1,1,1, 1,2,2,2,3,3,3,3,4,4,4,5,5,5 ...)
我尝试使用以下REP(),但有以下错误:
<代码> REP中的错误(3,seq(1:36)):参数'times'不正确
我需要使用什么公式来正确生成我想要的向量?
I am trying to create a vector where I have 3 repetitions of the number 1, then 3 repetitions of the number 2, and so on up to, for instance, 3 repetitions of the number 36.
c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5...)
I have tried the following use of rep() but got the following error:
Error in rep(3, seq(1:36)) : argument 'times' incorrect
What formulation do I need to use to properly generate the vector I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者甚至更好,正如@Wimpel 在评论中提到的那样,使用rep 函数的每个参数。
输出
Or even better as @Wimpel mentioned in the comments, use the each argument of the rep function.
output
这个应该起作用。但是可能不是最优雅的。
或者使用重复函数正确(请参阅文档(
rep?rep
)有关参数每个
):This one should work. However probably not the most elegant.
alternatively using the rep function properly (see documentation (
?rep
for argumenteach
):rep
方法是可取的(请参阅现有答案),这里还有其他选项:
rep
approach is preferable (see existing answers)Here are some other options: