Split a Vector By Split Points

Usage

split_vector(x, split = "", include = FALSE, regex = FALSE, ...)

Arguments

x
A vector with split points.
split
A vector of places (elements) to split on or a regular expression if regex argument is TRUE.
include
An integer of 1 (split character(s) are not included in the output), 2 (split character(s) are included at the beginning of the output), or 3 (split character(s) are included at the end of the output).
regex
logical. If TRUE regular expressions will be enabled for split argument.
...
other arguments passed to grep and grepl.

Value

Returns a list of vectors.

Description

Splits a vector into a list of vectors based on split points.

Examples

set.seed(15) x <- sample(c("", LETTERS[1:10]), 25, TRUE, prob=c(.2, rep(.08, 10))) split_vector(x)
$`1` [1] "C" $`2` [1] "A" "C" "D" "A" "I" "B" "H" "I" $`3` [1] "C" "E" "H" "J" "J" "E" "A" $`4` [1] "I" "I" "I" "G" $`5` [1] "F"
split_vector(x, "C")
$`1` [1] "" "A" $`2` [1] "D" "A" "I" "B" "H" "I" "" $`3` [1] "E" "H" "J" "J" "E" "A" "" "I" "I" "I" "G" "" "F"
split_vector(x, c("", "C"))
$`1` [1] "A" $`2` [1] "D" "A" "I" "B" "H" "I" $`3` [1] "E" "H" "J" "J" "E" "A" $`4` [1] "I" "I" "I" "G" $`5` [1] "F"
split_vector(x, include = 0)
$`1` [1] "C" $`2` [1] "A" "C" "D" "A" "I" "B" "H" "I" $`3` [1] "C" "E" "H" "J" "J" "E" "A" $`4` [1] "I" "I" "I" "G" $`5` [1] "F"
split_vector(x, include = 1)
$`1` [1] "C" $`2` [1] "" "A" "C" "D" "A" "I" "B" "H" "I" $`3` [1] "" "C" "E" "H" "J" "J" "E" "A" $`4` [1] "" "I" "I" "I" "G" $`5` [1] "" "F"
split_vector(x, include = 2)
[[1]] [1] "C" "" [[2]] [1] "A" "C" "D" "A" "I" "B" "H" "I" "" [[3]] [1] "C" "E" "H" "J" "J" "E" "A" "" [[4]] [1] "I" "I" "I" "G" "" [[5]] [1] "F"
set.seed(15) x <- sample(1:11, 25, TRUE, prob=c(.2, rep(.08, 10))) split_vector(x, 1)
$`1` [1] 4 $`2` [1] 2 4 5 2 10 3 9 10 $`3` [1] 4 6 9 11 11 6 2 $`4` [1] 10 10 10 8 $`5` [1] 7
## relationship to `loc_split` all.equal( split_vector(x, include = 1), loc_split(x, which(x == ""), names=1:6) )
Warning message: length of `names` muse be equal to length of `locs` + 1; ignoring `names`
[1] "names for target but not for current"

See also

loc_split, run_split

Author

Matthew Flickinger and Tyler Rinker .