Skip to content

Instantly share code, notes, and snippets.

@opplatek
Created December 2, 2021 22:47
Show Gist options
  • Save opplatek/1874f044d3bbed0cf19aedf86fb3d6fe to your computer and use it in GitHub Desktop.
Save opplatek/1874f044d3bbed0cf19aedf86fb3d6fe to your computer and use it in GitHub Desktop.
Splitting vector into smaller chunks by a separator and extracting chunk(s)
#
# Splitting vector into smaller chunks by a separator and extracting chunk(s)
#
split_vector <- function (x, sep, column){
# sapply(x, function(x) strsplit(x, split = sep, fixed = T)[[1]][column], USE.NAMES = F)
unlist(lapply(x, function(x) strsplit(x, sep, fixed = T)[[1]][column]))
}
# Test
vect <- c("a.b.c", "d.e.f")
split_vector(vect, ".", 2)
[1] "b" "e"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment