Check your understanding

Quiz

You have finished the text-and-time section: manipulating strings with stringr, writing regular expressions, parsing dates with lubridate, and ordered categories with forcats. This short quiz checks what stuck. Pick an answer to continue; you can retry until it clicks. The last two steps are live R, run them and tinker.

Question 1 of 8

What str_detect returns

You run str_detect(c("apple", "pear", "grape"), "ap"). What comes back?

Right. str_detect() reports whether each string contains the pattern, so you can use it to filter.
That is str_locate(); str_detect() gives TRUE or FALSE.
Question 2 of 8

A digit in a regex

In an R string, how do you write the regular expression that matches a single digit?

Correct. R sees \\ as one backslash, so the regex engine receives \d, the digit class.
That matches the literal letters d, i, g, i, t; the digit class is \\d or [0-9].
Question 3 of 8

First match or all of them

str_replace() and str_replace_all() differ how?

Right. Reach for the _all variant when a pattern can appear more than once.
It replaces all matches, not just the last.
Question 4 of 8

Parsing a date

You have the text "2026-03-08" and want a real date you can do arithmetic on. With lubridate, which call parses it?

Correct. lubridate's parser is named for the order of the parts, so ymd fits this text.
That keeps it as text; you want a Date object.
Question 5 of 8

What a factor's levels are

You make factor(c("low", "high", "low")). What are its levels?

Right. A factor stores the full set of permitted categories, which is what lets you order and count them cleanly.
Levels are the defining feature of a factor.
Question 6 of 8

Anchoring a pattern

In a regular expression, what does the ^ at the start of a pattern mean?

Right. ^abc matches only when the string begins with abc; pair it with $ to anchor the end.
Repetition is * or +; ^ anchors position.
Run it: pull out a pattern

stringr in live R

Extract the first run of digits from each string. Run it, then change the pattern to "[a-z]+" to grab letters instead.

RInteractive R
library(stringr) orders <- c("order 238 shipped", "ref 7 pending", "no number here") str_extract(orders, "[0-9]+")

  

str_extract() returns the first match of the pattern in each string, or NA when there is none, as in the last entry.

Run it: do date arithmetic

lubridate in live R

Parse two dates and subtract them to get the gap in days. Run it, then change one date and run again.

RInteractive R
library(lubridate) borrowed <- ymd("2026-03-01") returned <- ymd("2026-03-15") returned - borrowed

  

Once text becomes a real date with ymd(), ordinary subtraction gives you a meaningful number of days.

Section complete

Nice work. You can detect and replace text with stringr, write a digit class with the doubled backslash, tell str_replace from str_replace_all, parse dates with the right lubridate function, describe a factor's levels, and anchor a pattern with ^. Next section: iteration with the apply family and purrr.