Join and Split
str_c(..., sep = "", collapse = NULL) Join
multiple strings into a single string.
str_c(letters, LETTERS)
str_c(..., sep = "", collapse = NULL) Collapse a
vector of strings into a single string.
str_c(letters, collapse = "")
str_dup(string, times) Repeat strings times
times. str_dup(fruit, times = 2)
str_split_fixed(string, pattern, n) Split a
vector of strings into a matrix of substrings
(splitting at occurrences of a pattern match).
Also str_split to return a list of substrings.
str_split_fixed(fruit, " ", n=2)
glue::glue(..., .sep = "", .envir =
parent.frame(), .open = "{", .close = "}") Create
a string from strings and {expressions} to
evaluate. glue::glue("Pi is {pi}")
glue::glue_data(.x, ..., .sep = "", .envir =
parent.frame(), .open = "{", .close = "}") Use a
data frame, list, or environment to create a
string from strings and {expressions} to
evaluate. glue::glue_data(mtcars,
"{rownames(mtcars)} has {hp} hp")
Mutate Strings
str_sub() <- value. Replace substrings by
identifying the substrings with str_sub() and
assigning into the results.
str_sub(fruit, 1, 3) <- "str"
str_replace(string, pattern, replacement)
Replace the first matched pattern in each
string. str_replace(fruit, "a", "-")
str_replace_all(string, pattern,
replacement) Replace all matched patterns
in each string. str_replace_all(fruit, "a", "-")
str_to_lower(string, locale = "en")
1
Convert
strings to lower case.
str_to_lower(sentences)
str_to_upper(string, locale = "en")
1
Convert
strings to upper case.
str_to_upper(sentences)
str_to_title(string, locale = "en")
1
Convert
strings to title case. str_to_title(sentences)
str_conv(string, encoding) Override the
encoding of a string. str_conv(fruit,"ISO-8859-1")
str_view(string, pattern, match = NA) View
HTML rendering of first regex match in each
string. str_view(fruit, "[aeiou]")
str_view_all(string, pattern, match = NA) View
HTML rendering of all regex matches.
str_view_all(fruit, "[aeiou]")
str_wrap(string, width = 80, indent = 0, exdent
= 0) Wrap strings into nicely formatted
paragraphs. str_wrap(sentences, 20)
RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at stringr.tidyverse.org • Diagrams from @LVaudor ! • stringr 1.2.0 • Updated: 2017-10
Work with strings with stringr : : CHEAT SHEET
Detect Matches
str_detect(string, pattern) Detect the
presence of a pattern match in a string.
str_detect(fruit, "a")
str_which(string, pattern) Find the indexes of
strings that contain a pattern match.
str_which(fruit, "a")
str_count(string, pattern) Count the number
of matches in a string.
str_count(fruit, "a")
str_locate(string, pattern) Locate the
positions of pattern matches in a string. Also
str_locate_all. str_locate(fruit, "a")
Manage Lengths
str_length(string) The width of strings (i.e.
number of code points, which generally equals
the number of characters). str_length(fruit)
str_pad(string, width, side = c("le", "right",
"both"), pad = " ") Pad strings to constant
width. str_pad(fruit, 17)
str_trunc(string, width, side = c("right", "le",
"center"), ellipsis = "...") Truncate the width of
strings, replacing content with ellipsis.
str_trunc(fruit, 3)
str_trim(string, side = c("both", "le", "right"))
Trim whitespace from the start and/or end of a
string. str_trim(fruit)
Helpers
str_order(x, decreasing = FALSE, na_last =
TRUE, locale = "en", numeric = FALSE, ...)
1
Return
the vector of indexes that sorts a character
vector. x[str_order(x)]
str_sort(x, decreasing = FALSE, na_last = TRUE,
locale = "en", numeric = FALSE, ...)
1
Sort a
character vector.
str_sort(x)
Order Strings
The stringr package provides a set of internally consistent tools for working with character strings, i.e. sequences of characters surrounded by quotation marks.
str_sub(string, start = 1L, end = -1L) Extract
substrings from a character vector.
str_sub(fruit, 1, 3); str_sub(fruit, -2)
str_subset(string, pattern) Return only the
strings that contain a pattern match.
str_subset(fruit, "b")
str_extract(string, pattern) Return the first
pattern match found in each string, as a vector.
Also str_extract_all to return every pattern
match. str_extract(fruit, "[aeiou]")
str_match(string, pattern) Return the first
pattern match found in each string, as a
matrix with a column for each ( ) group in
pattern. Also str_match_all.
str_match(sentences, "(a|the) ([^ ]+)")
1
See bit.ly/ISO639-1 for a complete list of locales.