【问题标题】:a function to create a sequence of integers with each integer repeated a given number of times [in R] [duplicate]一个创建整数序列的函数,每个整数重复给定次数[在R中] [重复]
【发布时间】: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 function sequence


    【解决方案1】:

    我相信rep() 可以完全满足您的需求:

    rep(1:3, times = c(2, 5, 4))
    # [1] 1 1 2 2 2 2 2 3 3 3 3
    

    【讨论】:

    • 我的错误是我将times 向量参数传递给each 参数。
    猜你喜欢
    • 2016-04-19
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2014-05-11
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    相关资源
    最近更新 更多