strsplit: Improved strsplit function

View source: R/strsplit.R

strsplitR Documentation

Improved strsplit function

Description

This functions uses strsplit and adds the possibility to split and keep the delimiter after or before the given split. Or you can split between two given splits and keep both.

Usage

strsplit(x, split, type = "remove", perl = FALSE, ...)

Arguments

x

character vector, each element of which is to be split. Other inputs, including a factor, will give an error.

split

character vector (or object which can be coerced to such) containing regular expression(s) (unless fixed = TRUE) to use for splitting. If empty matches occur, in particular if split has length 0, x is split into single characters. If split has length greater than 1, it is recycled along x.

type

a charachter. Either to "remove" or keep the delimiter "before", "between" or "remove" the split.

perl

logical. Should Perl-compatible regexps be used? Is TRUE for all but "remove".

...

other inputs passed along to strsplit

Value

A list of the same length as x, the i-th element of which contains the vector of splits of x[i].

Author(s)

Jakob Gepp

See Also

strsplit or stackoverflow for more details.

Examples

x <- c("3D/MON&SUN")
strsplit(x, "[/&]")
# [[1]]
# [1] "3D"  "MON" "SUN"

strsplit(x, "[/&]", type = "before")
# [[1]]
# [1] "3D"  "/MON" "&SUN"

strsplit(x, "[/&]", type = "after")
# [[1]]
# [1] "3D/"  "MON&" "SUN"

x <- c("3D/MON&SUN 2D/MON&SUN")
strsplit(x, split("/", "M"))
# [[1]]
# [1] "3D"         "MON&SUN 2D" "MON&SUN"


STATWORX/helfRlein documentation built on Feb. 12, 2024, 2:21 a.m.