Cull (extract) patterns from strings.

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

Value

Character vector.

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

See also

Examples

string_cull(rownames(mtcars), '^M|a')
#> [1] "M" "M" "a" NA "a" "a" NA "M" "M" "M" "M" "M" "M" "M" "a" "a" "a" "a" "a" #> [20] "a" "a" "a" "a" "a" "a" "a" NA "a" "a" "a" "M" NA
string_cull(rownames(mtcars), '^M|a', get_all = TRUE)
#> [1] "M,a,a" "M,a,a,a" "a" NA "a" "a,a" NA #> [8] "M" "M" "M" "M" "M" "M" "M" #> [15] "a,a" "a" "a" "a" "a" "a,a" "a,a" #> [22] "a" "a" "a,a" "a" "a" NA "a" #> [29] "a,a" "a" "M,a,a,a" NA
string_left(rownames(mtcars), 3)
#> [1] "Maz" "Maz" "Dat" "Hor" "Hor" "Val" "Dus" "Mer" "Mer" "Mer" "Mer" "Mer" #> [13] "Mer" "Mer" "Cad" "Lin" "Chr" "Fia" "Hon" "Toy" "Toy" "Dod" "AMC" "Cam" #> [25] "Pon" "Fia" "Por" "Lot" "For" "Fer" "Mas" "Vol"
string_right(rownames(mtcars), 3)
#> [1] "RX4" "Wag" "710" "ive" "out" "ant" "360" "40D" "230" "280" "80C" "0SE" #> [13] "0SL" "SLC" "ood" "tal" "ial" "128" "vic" "lla" "ona" "ger" "lin" "Z28" #> [25] "ird" "1-9" "4-2" "opa" "a L" "ino" "ora" "42E"
string_mid(rownames(mtcars), 2, 2)
#> [1] "azd" "azd" "ats" "orn" "orn" "ali" "ust" "erc" "erc" "erc" "erc" "erc" #> [13] "erc" "erc" "adi" "inc" "hry" "iat" "ond" "oyo" "oyo" "odg" "MC " "ama" #> [25] "ont" "iat" "ors" "otu" "ord" "err" "ase" "olv"
string_positions(rownames(mtcars), c(1, 3, 5))
#> 1 3 5 #> [1,] "M" "z" "a" #> [2,] "M" "z" "a" #> [3,] "D" "t" "u" #> [4,] "H" "r" "e" #> [5,] "H" "r" "e" #> [6,] "V" "l" "a" #> [7,] "D" "s" "e" #> [8,] "M" "r" " " #> [9,] "M" "r" " " #> [10,] "M" "r" " " #> [11,] "M" "r" " " #> [12,] "M" "r" " " #> [13,] "M" "r" " " #> [14,] "M" "r" " " #> [15,] "C" "d" "l" #> [16,] "L" "n" "o" #> [17,] "C" "r" "s" #> [18,] "F" "a" " " #> [19,] "H" "n" "a" #> [20,] "T" "y" "t" #> [21,] "T" "y" "t" #> [22,] "D" "d" "e" #> [23,] "A" "C" "J" #> [24,] "C" "m" "r" #> [25,] "P" "n" "i" #> [26,] "F" "a" " " #> [27,] "P" "r" "c" #> [28,] "L" "t" "s" #> [29,] "F" "r" " " #> [30,] "F" "r" "a" #> [31,] "M" "s" "r" #> [32,] "V" "l" "o"
string_positions(rownames(mtcars), c(1, 3, 5), join = TRUE)
#> [1] "Mza" "Mza" "Dtu" "Hre" "Hre" "Vla" "Dse" "Mr " "Mr " "Mr " "Mr " "Mr " #> [13] "Mr " "Mr " "Cdl" "Lno" "Crs" "Fa " "Hna" "Tyt" "Tyt" "Dde" "ACJ" "Cmr" #> [25] "Pni" "Fa " "Prc" "Lts" "Fr " "Fra" "Msr" "Vlo"