Skip to content

Instantly share code, notes, and snippets.

@btupper
Created November 16, 2016 13:24
Show Gist options
  • Save btupper/20879e0b46e5ed63d402d7cff424dbb7 to your computer and use it in GitHub Desktop.
Save btupper/20879e0b46e5ed63d402d7cff424dbb7 to your computer and use it in GitHub Desktop.
Split a vector into a list of smaller vectors
#' Split a vector into groups of MAX (or possibly fewer)
#'
#' @param v vector or list to split
#' @param MAX numeric the maximum size per group
#' @return a list of the vector split into groups
split_vector <- function(v, MAX = 200){
nv <- length(v)
if (nv <= MAX) return(list('1' = v))
split(v, findInterval(1:nv, seq(from = 1, to = nv, by = MAX)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment