首页 > 解决方案 > 如何在 R 中的另一个向量元素中按次重复向量元素?

问题描述

我想重复每一个

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

解决方案


差不多好了:

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

推荐阅读