【发布时间】:2021-05-12 07:58:45
【问题描述】:
R中是否有生成整数序列的函数,其中每个整数重复预定的次数?
例子:
# 1 repeated twice, 2 repeated 5 times, 3 repeated 4 times and so on
1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3
我知道以下功能,但它没有达到目的:
rep(1:5, each=3)
【问题讨论】:
R中是否有生成整数序列的函数,其中每个整数重复预定的次数?
例子:
# 1 repeated twice, 2 repeated 5 times, 3 repeated 4 times and so on
1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3
我知道以下功能,但它没有达到目的:
rep(1:5, each=3)
【问题讨论】:
我相信rep() 可以完全满足您的需求:
rep(1:3, times = c(2, 5, 4))
# [1] 1 1 2 2 2 2 2 3 3 3 3
【讨论】:
times 向量参数传递给each 参数。