Search code examples
rrep

R - Make a repetitive sequence with 'rep'


I'm wondering if there is a simpler way to make a list with, for example 10 '4', 20 '6' and 30 '3' then writing it by hand (example <- c(4,4,4,4,...)) with the function 'rep'. I know i can repeat a certain sequence n times and each by n times too, but i don't know how can i make one with different amounts of each number.


Solution

  • Just use rep with both arguments being the desired vectors:

    x <- rep(c(4, 6, 3), c(10, 20, 30))
    table(x)
    
     3  4  6 
    30 10 20