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

Viewed 28
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?

1 Answers

We could use rep():

rep(d, n)

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