0

Let see the example

There is a vector of 100 dates of observations

 x <- 1:100

I would like to split it on chunks related to the percent of original data ("sum" should be "1").

  p <- c(30, 25, 20, 15, 10)
  sum(p)

The "p" is a percent of original data (30% and etc.)

The desired output is chunks of observations with variable size:

 result <- list(list(1:30), list(31:55), list(56:75), list(76:90), list(91:100))

This is useful for parallel processing when the first chunks have bigger size than the next ones.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Andrii
  • 2,843
  • 27
  • 33
  • @akrun [You can instantly reopen any question closed as a duplicate that was originally asked with a tag you have a gold badge for](https://meta.stackexchange.com/a/231212/220122). [if you have a gold tag-badge for a tag associated with a question that's been closed as a duplicate, you can edit the duplicate links to replace, add, remove or re-arrange them](https://meta.stackexchange.com/q/291824/220122). – Henrik May 08 '19 at 19:44
  • @Henrik I am aware of that and it is a trap to flag it – akrun May 08 '19 at 19:46

1 Answers1

0

We can do this with split

result1 <- lapply(unname(split(x, rep(seq_along(p), p))), function(x) list(x))
identical(result, result1)
#[1] TRUE

Or another option with Map

Map(function(x, y) list(x:y), c(1, cumsum(p)[-length(p)]+1), cumsum(p) )
akrun
  • 874,273
  • 37
  • 540
  • 662