Home > Net >  generating integer sequences
generating integer sequences

Time:03-07

How would I take the even values from 18 to 40 and repeat the sequence until I have 30 observations?

i.e. (18, 20, 22, 24, 26, 28, 30, 32, …40, 18, 20, 22, 24, …40, 18, …)

CodePudding user response:

We may use rep with seq i.e. seq to return the sequencees from 18 to 40 in intervals of 2 and then replicate the vector derived from seq to recycle till the length of the replicated vector reaches 30

rep(seq(18, 40, by = 2), length.out = 30)

Or as @I_O commented (there will be a warning message as the lengths are not the same)

integer(30)   9:20 * 2

-output

[1] 18 20 22 24 26 28 30 32 34 36 38 40 18 20 22 24 26 28 30 32 34 36 38 40 18 20 22 24 26 28

CodePudding user response:

A short alternative using modular math:

18   2 * (0:29 %% 12)
#>  [1] 18 20 22 24 26 28 30 32 34 36 38 40 18 20 22 24 26 28 30 32 34 36
#> [23] 38 40 18 20 22 24 26 28
  •  Tags:  
  • r
  • Related