string_cull | R Documentation |
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)
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 |
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 ( |
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).
Character vector.
https://github.com/robertschnitman/stringops
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.