generate regularly incremented sequences

I am trying to generate the following sequence

0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23

The input would be starting number 0, sub-sequence length 4, sequence-spacing 10, and number of sub-sequences 3. The length of the full sequence would be 4 x 3 = 12.

Below is what I have using purrr functions. I am still new to map and your comments or suggestions are welcome.

library(tidyverse)
seq.int(from = 0, by = 10, length.out = 3) %>% 
  map(function(x)(seq.int(from = x, length.out = 4))) %>% 
  reduce(c)
#>  [1]  0  1  2  3 10 11 12 13 20 21 22 23

Created on 2019-03-13 by the reprex package (v0.2.1)

Do you have to use purrr?

You can simply do rep(x = ((0:2) * 10), each = 4) + 0:3 to generate the desired sequence.

5 Likes

Of course not. It is just what I was learning recently.

Thanks for showing the each = argument . Very straightforward solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.