0

Im interested in generating a list with a sequencing of numbers like in this fashion:

list(1:3, 4:6, 7:9)

I have tried variations of using list and seq without luck and using repetition like rep(list(1:3), 3) gets me the right format but without the advancing sequencing so I'm at a loss to what probably is a really simple problem. Thanks in advance.

Krl74
  • 11
  • 1
  • 2
    This isn't clear. What is wrong with `list(1:3, 4:6, 7:9)`? Presumably there is a more general problem you are trying to solve, but if so what is it? – John Coleman Sep 14 '19 at 16:01
  • Sorry I should have added im trying to create a list from a long sequence of numbers, for arguments sake 1:300. so 1:3 in list[1], 4:6 in List[2] etc – Krl74 Sep 14 '19 at 16:06
  • Try `my_list <- c(list(1:3), list(4:6), list(7:9))` then. – deepseefan Sep 14 '19 at 16:07
  • Thanks a lot. That solved my issue – Krl74 Sep 14 '19 at 16:19
  • The provided question is sufficient to achieve your goal. But I leave the following for you. `x <- 1:9; y <- seq(from = 1, to = 9, by = 3); split(x, f = findInterval(x = x, vec = y))`. – jazzurro Sep 14 '19 at 16:19

1 Answers1

1

We can use gl to create a grouping variable for splitting the vector

split(x, as.integer(gl(length(x), 3, length(x))))

data

x <- 1:9
akrun
  • 874,273
  • 37
  • 540
  • 662