strSplit: Split the elements of a character vector into a matrix

View source: R/strSplit.R

strSplitR Documentation

Split the elements of a character vector into a matrix

Description

Split the elements of a character vector into a matrix

Usage

strSplit(x, split, fixed = TRUE, n = Inf)

Arguments

x

character. Character vector that does not contain NA 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). If TRUE, pattern is a string to be matched as is. Otherwise, will match by regular expression.

n

Inf or integer(1). Maximum number of strings to return. If Inf, return all strings defined by split boundary. When set, function will split up to this number.

Value

matrix. Character matrix split into columns.

Note

Updated 2023-09-22.

See Also

  • Infinite splits: strsplit().

  • Finite splits: gregexpr(), substr().

  • stringi::stri_split_fixed().

  • stringr::str_split_fixed().

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)

## 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)

## 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)

## 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)

acidgenomics/acidbase documentation built on Jan. 26, 2024, 4:26 p.m.