Search code examples
rseq

looping a sequence of numbers


I need to generate the following sequence of numbers:

from 101 to 124 then
from 201 to 224

and so on. I need to repeat this pattern 7 times, up to 724.

I know I can simply use

c(101:124,201:224, ...)

but I suspect there is an easier way. Maybe a loop?


Solution

  • We can try seq with sapply

    c(sapply(seq(101, 700, by = 100), function(i) i:(i+23)))
    

    Or we can use rep

    (101:124) +rep(0:6, each = 24)*100
    #[1] 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    #[19] 119 120 121 122 123 124 201 202 203 204 205 206 207 208 209 210 211 212
    #[37] 213 214 215 216 217 218 219 220 221 222 223 224 301 302 303 304 305 306
    #[55] 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    #[73] 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    #[91] 419 420 421 422 423 424 501 502 503 504 505 506 507 508 509 510 511 512
    #[109] 513 514 515 516 517 518 519 520 521 522 523 524 601 602 603 604 605 606
    #[127] 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    #[145] 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    #[163] 719 720 721 722 723 724