Skip to content

Instantly share code, notes, and snippets.

@wdkrnls
Created January 31, 2017 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wdkrnls/9e49a18ccf58f3bcd30dd9b7cba0e684 to your computer and use it in GitHub Desktop.
Save wdkrnls/9e49a18ccf58f3bcd30dd9b7cba0e684 to your computer and use it in GitHub Desktop.
Chunk a text string into k strings.
chunk_text <- function(txt, k) {
n <- nchar(txt)
q <- n %/% k
r <- n %% k
unlist(lapply(seq.int(1, k), function(i) {
substr(txt, start = (i - 1)*q + min(i - 1, r) + 1, stop = i * q + min(i, r))
}), recursive = FALSE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment