0

Is there a function to generate a sequence of integers in R where each integer is repeated a predefined number of times?

Example:

# 1 repeated twice, 2 repeated 5 times, 3 repeated 4 times and so on    
1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3

I know the following function but it does not serve the purpose:

rep(1:5, each=3)
Mainul Islam
  • 1,196
  • 3
  • 15
  • 21

1 Answers1

2

I believe rep() can do exactly what you need:

rep(1:3, times = c(2, 5, 4))
# [1] 1 1 2 2 2 2 2 3 3 3 3
zx8754
  • 52,746
  • 12
  • 114
  • 209
Andrey Shabalin
  • 4,389
  • 1
  • 19
  • 18