Stringr
Overview
Stringr is the main string manipulation package in tidyverse.
Stringr focusses on the most important and comonly used string manipulation functions. It is based on stringi which has a comprehensive set of functions.
This website aims to quickly cover some of the stringr functions and uses. Therefore, there are a lot more stringr functions than covered here. Please check the below link for the full list.
If you are unfamiliar with strings please see:
Sections
There are many sections for stringr. These are summarised below.
Viewing strings
str_view(): View the raw contents of a string.- Includes information about various special characters and how to escape them with
\.
- Includes information about various special characters and how to escape them with
Character manipulation
These functions allow you to manipulate strings in vectors.
str_length(): Determine the length of a string (i.e. count the number of characters in a string).str_sub(): Extract or modify a substring using a start and end position.str_dup(): Duplicate a string a specified number of times.
Combining strings
These functions allow you combine strings into one scalar or vector.
str_c(): Combine strings together (similar topaste0()).str_flatten(): Flatten a vector of strings into one string.str_glue(): Similar tostr_c()but allows you to include variables within the strings to combine.- Variable names are included in the strings with curly braces like so:
{variable_name}
- Variable names are included in the strings with curly braces like so:
White space tools
These functions allow you to add, remove, and manipulate whitespaces (e.g. spaces and tabs).
str_pad(): Add padding characters to strings shorter than a specified length. The padded strings will all have a length equal to the length.str_trim(): Remove all white spaces from the start and end of strings.
Pattern matching
These functions allow you to carry out various tasks with regular expressions.
str_subset(): Subset vector to elements that match a pattern/regular expression.str_detect(): Return logical vector showing which elements match (TRUE) a pattern/regular expression or not (FALSE).str_count(): Count number of matches to a pattern/regular expression.
For an introduction to regular expressions please see: