string_cull: Cull/extract patterns from strings

View source: R/string_cull.R

string_cullR Documentation

Cull/extract patterns from strings

Description

Cull (extract) patterns from strings.

Usage

string_cull(s, pattern, get_all = FALSE, collapse = ',', ...)
string_left(s, n)
string_right(s, n)
string_mid(s, start, n)
string_positions(s, positions, join = FALSE)

Arguments

s

A string (character) vector.

pattern

A regular expression pattern.

get_all

Whether to get all matches (TRUE) or not (FALSE).

collapse

A separator in the case of multiple matches.

...

Inputs get passed to gregexpr().

start

The position in a string at which to start.

n

The number of characters.

positions

An integer vector of character positions to extract from a string.

join

Determines whether to have columns for each position (FALSE) or join the extracted characters together (TRUE).

Details

The function string_cull() culls (or extracts) a pattern from a string vector–if no pattern is found, NA is returned. Multiple pattern matches are separated by the collapse() input if get_all = TRUE.

The functions string_left(), string_right(), string_mid() act the same as Excel's LEFT(), RIGHT, MID() functions, respectively: they extract a number of characters at a specified starting point (from the beginning for string_left(), from the right for string_right(), and from a specific position for string_mid()).

The function string_positions() pulls the character(s) at specified positions. If join = FALSE, then the extracted characters are split into columns; otherwise, they are concatenated together into a single vector.

The synonym pattern of these functions are s_*() and *() (replace asterisks with cull, left, right, mid, and positions).

Value

Character vector.

See Also

https://github.com/robertschnitman/stringops

Examples

string_cull(rownames(mtcars), '^M|a')
string_cull(rownames(mtcars), '^M|a', get_all = TRUE)
string_left(rownames(mtcars), 3)
string_right(rownames(mtcars), 3)
string_mid(rownames(mtcars), 2, 2)
string_positions(rownames(mtcars), c(1, 3, 5))
string_positions(rownames(mtcars), c(1, 3, 5), join = TRUE)


robertschnitman/stringops documentation built on Aug. 15, 2022, 3:46 p.m.