Home > Software engineering >  repeat each element of vector by number specified in a numeric vector
repeat each element of vector by number specified in a numeric vector

Time:03-22

d <- c('a', 'b', 'c')
n <- c(1, 2, 5)

I would like to produce output:

res <- c('a', 'b', 'b', 'c', 'c', 'c', 'c', 'c')

Is there a function which can use d and n to produce such a result?

CodePudding user response:

We could use rep():

rep(d, n)

[1] "a" "b" "b" "c" "c" "c" "c" "c"
  • Related