Search code examples
rrepeat

repeat each element of vector by number specified in a numeric vector


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?


Solution

  • We could use rep():

    rep(d, n)
    
    [1] "a" "b" "b" "c" "c" "c" "c" "c"