【问题标题】:How do I repeat vector elements by times in another vector elements in R?如何在 R 中的另一个向量元素中按次重复向量元素?
【发布时间】:2018-11-18 14:56:56
【问题描述】:

我想重复每一个

repeat.it <- c(0, 3951982, 7635488, 10986941)

按次:

repeat.times<- c(2L, 3L, 4L, 2L)

并得到结果:

0,0,3951982,3951982,3951982,7635488,7635488,7635488,7635488,10986941,10986941

我试过的代码: rep(repeat.it, each=repeat.times) 但这似乎给了我错误的结果。我怎样才能正确地做到这一点?

【问题讨论】:

    标签: r


    【解决方案1】:

    差不多了:

    rep(repeat.it, times = repeat.times)
    #  [1]        0        0  3951982  3951982  3951982  7635488  7635488
    #  [8]  7635488  7635488 10986941 10986941
    

    【讨论】:

    • @MAPK,它回答了你的问题吗?
    【解决方案2】:

    这是一个选项

    res <- mapply(rep, repeat.it, repeat.times)
    res <- unlist(res)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2021-07-28
      • 1970-01-01
      • 2020-10-18
      • 2014-05-21
      相关资源
      最近更新 更多