9.1 stringr: Strings

DATA 412/612 · Week 9 · PDF · Source (.qmd)

str_c() combines, str_sub() extracts, and case helpers change letters.

1. A string is text in quotes

stringr comes with library(tidyverse). Every function starts with str_. The first argument is always the string (or a character vector), so the functions pipe.

library(tidyverse)

string1 <- "This is a string"
string2 <- 'A quote "inside" a string uses the other quote mark'

Special characters use a backslash. In the console you see the escape. writeLines() prints what the string actually contains.

x <- c("\"", "\", "a
b")
x
## [1] "\""   "\"   "a
b"
writeLines(x)
## "
## \
## a
## b

\" is a double quote. \ is one backslash. is a newline. is a tab.


2. Length and pieces

str_length() counts characters, including spaces. NA stays NA.

str_length(c("a", "R for data science", NA))
## [1]  1 18 NA

str_c() pastes strings together. sep goes between the arguments. collapse pastes the elements of one vector into a single string.

str_c("x", "y")
## [1] "xy"
str_c("x", "y", sep = ", ")
## [1] "x, y"
str_c("prefix-", c("a", "b", "c"), "-suffix")
## [1] "prefix-a-suffix" "prefix-b-suffix" "prefix-c-suffix"
str_c(c("x", "y", "z"), collapse = ", ")
## [1] "x, y, z"

sep and collapse are easy to mix up. sep is for several arguments. collapse is for one vector.

If any piece is NA, the whole result is NA. That is not how paste() works. Turn the missing value into text first if you want it to stay in the string:

str_c("prefix-", c("a", NA, "c"), "-suffix")
## [1] "prefix-a-suffix" NA                "prefix-c-suffix"
str_c("prefix-", str_replace_na(c("a", NA, "c")), "-suffix")
## [1] "prefix-a-suffix"  "prefix-NA-suffix" "prefix-c-suffix"

3. str_sub() extracts and replaces

str_sub(string, start, end) takes a slice of characters. Counts are 1-based. Negative numbers count from the end. A too-long end just stops at the last character.

x <- c("Apple", "Banana", "Pear")
str_sub(x, 1, 3)
## [1] "App" "Ban" "Pea"
str_sub(x, -3, -1)
## [1] "ple" "ana" "ear"
str_sub(x, 1, 1)
## [1] "A" "B" "P"

Assignment replaces that slice. The rest of the string stays.

str_sub(x, 1, 1) <- "Z"
x
## [1] "Zpple"  "Zanana" "Zear"

4. Case

str_to_lower("THE WOODS")
## [1] "the woods"
str_to_upper("the woods")
## [1] "THE WOODS"
str_to_title("the woods are lovely")
## [1] "The Woods Are Lovely"

str_to_title() capitalizes each word. Locale matters for a few languages; the default is usually fine.


5. A first replacement

str_replace() changes the first match. str_replace_all() changes every match. A named vector is several replacements in one call. The names are the old pieces; the values are the new ones.

str_replace("back pack", "back", "foo")
## [1] "foo pack"
str_replace_all(c("back", "lack", "black"), c("back" = "foo", "lack" = "foo"))
## [1] "foo"  "foo"  "bfoo"

The second line also changes "black" to "bfoo", because "lack" sits inside it. Patterns that mean “this word only” wait for note 9.2.


6. Practice

  1. From full <- "To be or not to be, that is the question.", use str_sub() to pull two pieces and str_c() to join them so writeLines() prints a two-line quote.
  2. Replace "back" and "lack" with "foo" in one call. Try c("back", "lack") and then c("back", "lack", "black").