R/utils.R

Defines functions vec_split_interval

#' Chop up a vector into equal sized chunks
#'
#' From: \url{https://gist.github.com/sckott/4632735}
#'
#' @param x An input vector.
#' @param interval The length of the resulting vectors.
#'
#' @family vector tools
#' @export
#' @examples
#' vec <- c("a","b","d","e","f","g","h")
#' vec_split_interval(vec, 3)
vec_split_interval <- function(x, interval){
  splt                 <- rep(FALSE, interval)
  splt[1]              <- TRUE
  a <- ceiling(length(x) / length(splt))
  splt  <-  rep(splt, a)
  splt <- cumsum(splt)

  split(x, splt)
}
s-fleck/rdm documentation built on Nov. 16, 2020, 4:58 a.m.