x <- "We usually create strings by wrapping text in double quotes."
writeLines(x)We usually create strings by wrapping text in double quotes.
Spring 2026 | Data 2 (399)
Jeffrey M. Girard | Lecture 12a
Understand how R handles strings and special characters
Combine, separate, and extract parts of text data
Use regular expressions to detect and replace patterns within strings
Question: How does R handle text?
Text data is stored as strings (character vectors)
We create strings by wrapping text in double quotes ("as so") or single quotes ('as so')
str_ and are built specifically to handle messy textQuestion: How do we define text data in R?
We usually create strings by wrapping text in double quotes.
Question: How do we stick multiple strings together?
Question: How can we flexibly combine strings?
Yes, str_glue() lets us insert R variables directly into a string using curly braces {}. This is often much easier to read than str_c().
Question: How do we collapse a whole column into a single string?
We can use str_flatten() to combine a vector of multiple strings into one single string. We can also specify a separator.
Question: Which carriers operate out of each NYC airport?
# A tibble: 3 × 2
origin carriers
<chr> <chr>
1 EWR 9E, AA, AS, B6, DL, EV, MQ, OO, UA, US, VX, WN
2 LGA 9E, AA, B6, DL, EV, F9, FL, MQ, OO, UA, US, WN, YV
3 JFK 9E, AA, B6, DL, EV, HA, MQ, UA, US, VX
Question: How do we extract a specific range of characters?
We use str_sub(), which takes a start and end position. We can even use negative numbers to count backward from the end of the string.
# A tibble: 3 × 4
hex red green blue
<chr> <chr> <chr> <chr>
1 #FF0000 FF 00 00
2 #00FF00 00 FF 00
3 #0000FF 00 00 FF
Question: How do we split one messy column into multiple clean ones?
Question: Can we separate into rows instead of columns?
Question: Why is this useful in data science?
Question: What if our target isn’t a fixed position or a simple delimiter?
Regular Expressions (regex) are a concise language for describing patterns in strings. They are very powerful but can look like gibberish
str_view() to preview what our regex is matching. Matches will be surrounded by angle brackets: <...>The simplest regex pattern is just an exact match of characters.
Question: What if we only want matches at the start or end of a string?
We use anchors. ^ matches the start of the string, and $ matches the end.
Question: How do we match flexible patterns instead of exact matches?
.): Matches any single character[]): Matches any one of the characters listed\\d (digits) and \\s (spaces){n}: matches exactly n times+: matches one or more timesQuestion: How do we filter a dataset based on text patterns?
We can use str_detect() to ask if the pattern was detected
Question: How do we fix or replace specific text elements?
[1] "We often utilize fancy words when we could utilize simple ones."
Question: How do we delete parts of a string?
Sometimes strings have
too much white space
Question: Why is this useful in data science?
Data entered by humans in free-text fields is notoriously messy. We can string these tools together to sanitize it.
df |>
mutate(
clean_email =
email |>
str_squish() |> # Collapse whitespace
str_replace(" AT ", "@") |> # Replace AT with @
str_replace(" DOT ", ".") |> # Replace DOT with .
str_to_lower() |> # Make all lowercase
str_remove_all("\\s"), # Remove all spaces
dotedu = str_detect(clean_email, "\\.edu$")
)# A tibble: 4 × 3
email clean_email dotedu
<chr> <chr> <lgl>
1 "jane AT uni DOT edu" jane@uni.edu TRUE
2 "\njohn@gmail.com " john@gmail.com FALSE
3 "educator@khan.org" educator@khan.org FALSE
4 "SMITH@ college.edu " smith@college.edu TRUE