Split the elements of a character vector into a matrix
Arguments
- x
character
. Character vector that does not containNA
or empty strings.- split
character(1)
. String that defines the split boundary. The number of characters (nchar
) can be greater than 1 here. Regular expressions are intentionally not supported to keep this simple.- fixed
logical(1)
. IfTRUE
,pattern
is a string to be matched as is. Otherwise, will match by regular expression.- n
Inf
orinteger(1)
. Maximum number of strings to return. IfInf
, return all strings defined by split boundary. When set, function will split up to this number.
See also
Infinite splits:
strsplit()
.Finite splits:
gregexpr()
,substr()
.
Examples
## Infinite number of fixed splits.
x <- c("a__b__c", "d__e__f", "g__h__i")
mat <- strSplit(x = x, split = "__", fixed = TRUE, n = Inf)
print(mat)
#> [,1] [,2] [,3]
#> [1,] "a" "b" "c"
#> [2,] "d" "e" "f"
#> [3,] "g" "h" "i"
## Infinite number of regex splits.
x <- c("a_b_c_d", "e_f__g___h", "i__j__k__l")
mat <- strSplit(x = x, split = "_+", fixed = FALSE, n = Inf)
print(mat)
#> [,1] [,2] [,3] [,4]
#> [1,] "a" "b" "c" "d"
#> [2,] "e" "f" "g" "h"
#> [3,] "i" "j" "k" "l"
## Finite number of fixed splits.
x <- c("a__b__c", "d__e__f", "g__h__i")
mat <- strSplit(x = x, split = "__", fixed = TRUE, n = 2L)
print(mat)
#> [,1] [,2]
#> [1,] "a" "b__c"
#> [2,] "d" "e__f"
#> [3,] "g" "h__i"
## Finite number of regex splits.
x <- c("a_b_c_d", "e_f__g___h", "i__j__k__l")
mat <- strSplit(x = x, split = "_+", fixed = FALSE, n = 2L)
print(mat)
#> [,1] [,2]
#> [1,] "a" "b_c_d"
#> [2,] "e" "f__g___h"
#> [3,] "i" "j__k__l"