Skip to content

Instantly share code, notes, and snippets.

@wdkrnls
Created November 25, 2015 03:10
Show Gist options
  • Save wdkrnls/83b0118ecb660eacd20c to your computer and use it in GitHub Desktop.
Save wdkrnls/83b0118ecb660eacd20c to your computer and use it in GitHub Desktop.
#' Clever chunking algorithm into roughly equal parts
#' http://stackoverflow.com/questions/2130016/splitting-a-list-of-arbitrary-size-into-only-roughly-n-equal-parts
chunk <- function(x, k) {
n <- length(x)
q <- n %/% k
r <- n %% k
lapply(seq.int(1, k), function(i) {
x[seq(from = (i - 1)*q + min(i - 1, r) + 1, to = i * q + min(i, r))]
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment