This lab focuses on manipulating character strings using the
stringr package within the tidyverse suite. We
will examine functions for locating, modifying, and cleaning strings,
and introduce regular expressions for identifying patterns in character
data.
# Please install any packages you do not already have
library(stringr)
library(dplyr)
library(readr)
A string is a single element of a character variable (vector), or a standalone collection of characters enclosed by quotes.
x = "Single Element String"
y = c("apple", "banana", "pear") # Strings within a character vector
Strings have some similarities to other types of data. In particular, characters occupy positions within a string, and we can measure the length of each string.
str_length(x)
## [1] 21
str_length(y)
## [1] 5 6 4
length(y)
## [1] 3
Notice the distinction between str_length() and
length(). The former counts the characters within each
string, while the latter counts the number of elements in the
vector.
We can use the positions of individual characters to extract portions
of strings with str_sub():
str_sub(x, start = 1, end = 4)
## [1] "Sing"
str_sub(y, start = 1, end = 4)
## [1] "appl" "bana" "pear"
Positions can also be referenced from the end of a string using negative numbers:
str_sub(x, start = -4, end = -1)
## [1] "ring"
str_sub(y, start = -2, end = -1)
## [1] "le" "na" "ar"
When we combine different data types within an atomic vector, R generally converts the elements to a common type. In particular, the presence of a character string will cause numerical values to be stored as characters.
x1 = c(1, 2, 3)
typeof(x1)
## [1] "double"
x2 = c(1, 2, 3, "A")
typeof(x2)
## [1] "character"
The same occurs when we combine logical values with character strings:
y1 = c(TRUE, FALSE)
typeof(y1)
## [1] "logical"
y2 = c(TRUE, FALSE, "A")
typeof(y2)
## [1] "character"
Notice that missing values are handled differently. An
NA does not automatically cause a vector to be converted to
character:
y3 = c(1, 2, 3, NA)
typeof(y3)
## [1] "double"
Problems can arise when raw datasets contain character strings representing missing observations, annotations, or footnotes. Consider the following dataset:
my_data = read.csv("https://nfriedrichsen.github.io/data/char_dom.csv")
head(my_data)
## ID messy_x messy_y
## 1 1 100 50
## 2 2 90 40
## 3 3 85 55
## 4 4 90 45
## 5 5 110 55*
## 6 6 Missing 60
str(my_data)
## 'data.frame': 10 obs. of 3 variables:
## $ ID : int 1 2 3 4 5 6 7 8 9 10
## $ messy_x: chr "100" "90" "85" "90" ...
## $ messy_y: chr "50" "40" "55" "45" ...
Although the variables messy_x and messy_y
appear to contain numerical measurements, both are stored as character
variables. We cannot immediately perform numerical calculations on them
without first addressing the nonnumeric entries.
Fortunately, messy_x can be converted using
as.numeric(), while messy_y can be handled
using parse_number() from the readr
package:
# Coerce x to numeric
# Entries that cannot be converted become NA
as.numeric(my_data$messy_x)
## Warning: NAs introduced by coercion
## [1] 100 90 85 90 110 NA 115 NA 105 100
# Extract the numeric values from y
parse_number(my_data$messy_y)
## [1] 50 40 55 45 55 60 40 35 40 50
These functions behave differently. as.numeric()
attempts to convert each complete string into a number, while
parse_number() can retrieve a number even when other
characters are present. This is convenient, although it may also discard
information contained in annotations.
The stringr package contains dozens of string-processing
tools. We will focus on several commonly used functions:
| Function | Description |
|---|---|
str_sub() |
Extract a substring between specified positions |
str_detect() |
Identify whether a pattern occurs in a string |
str_locate() |
Return the start and end positions of the first match |
str_locate_all() |
Return the positions of all matches |
str_count() |
Count occurrences of a pattern |
str_replace() |
Replace the first occurrence of a pattern |
str_replace_all() |
Replace every occurrence of a pattern |
For illustration purposes, we will use the following vector:
fruits = c("Apple", "Pineapple", "Pear", "Orange", "Peach", "Banana")
String Detect
str_detect() returns TRUE or
FALSE for each element of a character vector depending on
whether it contains the target pattern.
str_detect(fruits, "ap")
## [1] FALSE TRUE FALSE FALSE FALSE FALSE
Because strings are case-sensitive, "ap" is found in
"Pineapple" but not in "Apple".
We can also use the logical results to select the corresponding elements:
fruits[str_detect(fruits, "ap")]
## [1] "Pineapple"
String Locate
str_locate() returns the starting and ending positions
of the first occurrence of a pattern:
str_locate(fruits, "an")
## start end
## [1,] NA NA
## [2,] NA NA
## [3,] NA NA
## [4,] 3 4
## [5,] NA NA
## [6,] 2 3
Notice that "Banana" contains two instances of
"an", but str_locate() identifies only the
first. To identify all occurrences, we can use
str_locate_all():
str_locate_all(fruits, "an")
## [[1]]
## start end
##
## [[2]]
## start end
##
## [[3]]
## start end
##
## [[4]]
## start end
## [1,] 3 4
##
## [[5]]
## start end
##
## [[6]]
## start end
## [1,] 2 3
## [2,] 4 5
The output is a list containing a matrix of positions for each string. This can be useful when we need to know exactly where a pattern occurs, but it is somewhat cumbersome if we only want to count matches.
For counting, the str_count() function provides a
simpler approach:
str_count(fruits, "an")
## [1] 0 0 0 1 0 2
# Total occurrences across the entire vector
sum(str_count(fruits, "an"))
## [1] 3
String Replace
str_replace() substitutes the first occurrence of a
pattern with another expression:
str_replace(fruits, "an", "XX")
## [1] "Apple" "Pineapple" "Pear" "OrXXge" "Peach" "BXXana"
If we want to replace every occurrence, we can instead use
str_replace_all():
str_replace_all(fruits, "an", "XX")
## [1] "Apple" "Pineapple" "Pear" "OrXXge" "Peach" "BXXXXa"
Compare the two results for "Banana", which contains
more than one match.
Question #1: Using appropriate stringr
functions:
Part A: identify which elements in the vector
fruits contain a lowercase "p". Then, use the
which() function and indices to print these fruits.
Part B: Count the total number of times a lowercase
"p" occurs in the vector fruits.
Question 2: A retailer uses product identification codes containing information about the product category, item number, and warehouse location. Consider the following vector:
products = c(
"EL-1042-DSM", "FD-2031-CHI", "CL-1508-NYC",
"EL-1095-CHI", "FD-2077-DSM", "CL-1542-DSM",
"EL-1123-NYC", "CL-1589-CHI", "FD-2145-NYC",
"EL-1178-DSM", "FD-2192-CHI", "CL-1624-NYC",
"EL-1210-CHI", "FD-2238-DSM", "CL-1673-DSM",
"FD-2281-NYC", "EL-1256-NYC", "CL-1715-CHI",
"FD-2324-DSM", "EL-1302-DSM"
)
The first two characters identify the product category
(EL = Electronics, FD = Food, CL
= Clothing), the middle four digits identify the product, and the final
three characters identify the warehouse.
(a) Use str_sub() to extract the
warehouse code from each product identifier.
(b) Identify all products belonging to the Electronics category. Return the complete product identifiers, rather than a logical vector.
(c) Determine how many products are associated with the DSM warehouse.
(d) The retailer is changing the warehouse code
DSM to DES. Modify the product identifiers
accordingly, without changing the original products
vector.
(e) Create a vector containing only the four-digit item numbers. Then convert them to numeric data.
When working with strings, it is often beneficial to
pre-process the data before performing other operations (this
means making it look prettier or otherwise easier to work with). For
example, we might standardize capitalization using
str_to_lower():
str_to_lower(fruits)
## [1] "apple" "pineapple" "pear" "orange" "peach" "banana"
Related functions allow us to convert strings to uppercase, title case, or sentence case:
str_to_upper(fruits)
## [1] "APPLE" "PINEAPPLE" "PEAR" "ORANGE" "PEACH" "BANANA"
str_to_title(fruits)
## [1] "Apple" "Pineapple" "Pear" "Orange" "Peach" "Banana"
str_to_sentence("aPPles AND Bananas are THE Most popular FRUits")
## [1] "Apples and bananas are the most popular fruits"
Another common issue is whitespace, including unwanted spaces at the beginning or end of a string or repeated spaces between words.
The str_squish() function removes leading and trailing
whitespace and reduces consecutive whitespace within a string to a
single space:
str_squish(" String A ")
## [1] "String A"
If we only want to remove leading and trailing whitespace, without
changing the spacing inside the string, we can use
str_trim():
str_trim(" String A ")
## [1] "String A"
Question #3: Use appropriate stringr
functions to complete the following.
Part A Beginning with the character string given
below, use str_squish(), str_to_title(), and
str_replace_all() to produce the string
"United_States".
q3 = c(" UNITED STATES ")
Part B Consider the following vector:
names = c(
"Rachel Nguyen ", " omar HADDAD",
"JAMES WILSON", " maria garcia",
"daniel kim", " Michael Thompson",
"CHINEDU okafor ", "Ashley Miller",
" jose martinez", "Hannah Patel",
"aLEXANDER JOHNSON ", "sarah chen ",
"Christopher Lee", " Emily Rodriguez "
)
Standardize the names so that each has appropriate capitalization, no leading or trailing spaces, and exactly one space between the first and last name.
Regular expressions, or regex, are sequences of characters
used to identify patterns within strings. At their simplest, regular
expressions may look exactly like the text we are trying to find. For
example, "ea" matches the same two characters in
"pear".
However, regular expressions can also describe more flexible
patterns. The period (.) is a special character, sometimes
called a wildcard, that matches any single character.
Consider the following example:
y = c("apple", "banana", "pear")
str_extract(y, ".a.")
## [1] NA "ban" "ear"
The pattern ".a." looks for an "a" with
exactly one character immediately before it and one character
immediately after it. Notice that the surrounding characters do not need
to be the same in every string.
We can also use str_detect() to identify which strings
contain such a pattern:
str_detect(y, ".a.")
## [1] FALSE TRUE TRUE
Here, str_extract() returns the matching portion of each
string, while str_detect() reports whether a match
exists.
By default, regular expressions can match a pattern anywhere within a string. Sometimes, however, we only want to match patterns at the beginning or end of a string.
Two special characters allow us to anchor our expressions:
^ anchors the pattern to the beginning of a
string.$ anchors the pattern to the end of a string.For example:
fruits[str_detect(fruits, "e$")] # Fruits ending in "e"
## [1] "Apple" "Pineapple" "Orange"
fruits[str_detect(fruits, "^P")] # Fruits beginning with "P"
## [1] "Pineapple" "Pear" "Peach"
These anchors can be combined with other regex characters to describe more specific patterns.
Question #4: Use the wildcard character,
., and anchoring to find all strings in fruits
that start with "P", followed by any character, followed by
"a".
The wildcard character is useful when we want to match any character, but we often want to restrict a match to a particular collection of characters.
Square brackets, [ ], define a character class.
A character class matches one character from the collection specified
inside the brackets.
For example:
str_detect(fruits, "[Pp]")
## [1] TRUE TRUE TRUE FALSE TRUE FALSE
The expression [Pp] matches either an uppercase
"P" or a lowercase "p". This allows us to
detect either capitalization without first converting the strings to a
common case.
We can also specify ranges of characters:
str_detect(fruits, "[A-Z]") # Any uppercase letter
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
str_detect(fruits, "[0-9]") # Any digit
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
The expression [A-Z] matches a single uppercase letter,
while [0-9] matches a single digit. These patterns can be
combined with anchors and other characters to describe more complicated
strings.
For example, consider the following vector of identifiers:
IDs = c("A123", "B456", "a789", "C12",
"D1234", "E567", "FXX", "g45")
Suppose we want to identify which strings begin with an uppercase letter followed immediately by a digit:
str_detect(IDs, "^[A-Z][0-9]")
## [1] TRUE TRUE FALSE TRUE TRUE TRUE FALSE FALSE
The ^ requires the match to begin at the start of the
string, [A-Z] matches one uppercase letter, and
[0-9] matches the digit immediately after it.
Notice, however, that this expression does not require the entire string to follow any particular format. It only specifies the first two characters.
Question 5
Consider the vector IDs defined above.
(a) Use str_detect() to identify which
strings begin with an uppercase letter.
(b) Construct a regular expression that identifies strings beginning with an uppercase letter followed by two digits. Which identifiers match your expression?
(c) Modify your expression from part (b) so that it identifies strings containing exactly one uppercase letter followed by two digits, with no additional characters. Explain the role of the anchors in your expression.
(d) Suppose lowercase letters should also be accepted as the first character. Modify your expression from part (c) to accommodate both uppercase and lowercase letters.
On Wednesday, we introduced regular expressions (regex) for
identifying patterns in character data. Today, we will expand on these
tools to identify, extract, and modify more complicated patterns in
strings. We will also briefly introduce working with dates and times in
R, before applying these techniques to several
data-processing problems.
The meta-characters * and + can be used to
match patterns with a flexible number of repeated characters:
* matches zero or more instances of
the preceding character or pattern.+ matches one or more instances of the
preceding character or pattern.? matches zero or one instance of the
preceding character or pattern.For example, consider the following strings containing different numbers of repeated characters:
strings = c("god", "good", "goood", "goooood", "gooooood!")
str_detect(strings, "goo*d")
## [1] TRUE TRUE TRUE TRUE TRUE
str_detect(strings, "goo+d")
## [1] FALSE TRUE TRUE TRUE TRUE
fruits[str_detect(fruits, "^A.*e$")]
## [1] "Apple"
The last expression finds strings beginning with "A",
followed by any number of characters, and ending in "e".
Notice how .* can match a sequence of characters of any
length.
We can also specify the number of repetitions using curly brackets:
| Expression | Meaning |
|---|---|
{n} |
Exactly n repetitions |
{n,} |
At least n repetitions |
{n,m} |
Between n and m repetitions |
For example, [0-9]{2} matches two consecutive digits,
while [0-9]{2,4} matches between two and four consecutive
digits. This provides a shorter way of writing the expressions we used
in Question 5.
IDs[str_detect(IDs, "^[A-Z][0-9]{2}$")]
## [1] "C12"
IDs[str_detect(IDs, "^[A-Z][0-9]{2,4}$")]
## [1] "A123" "B456" "C12" "D1234" "E567"
Question #6: Consider the following vector of responses from a customer satisfaction survey:
responses = c("good", "goood", "god", "goooood!", "Good",
"very good", "goodness", "goooooood", "bad", "GOOD")
Part A: Write an expression that identifies
responses containing "good" with at least two
consecutive o’s, allowing additional o’s. Your expression
should not match "god". Return the matching responses.
Part B: Modify your expression so that the
entire response must consist of "g",
between two and five "o" characters, and "d",
with no additional text or punctuation. Which responses match?
There are a few other special expressions that are useful when working with character data. Some of these are shortcuts for patterns we’ve already seen, while others allow us to match punctuation or combine alternatives.
| Expression | Meaning |
|---|---|
\\d |
Any digit |
\\s |
Any whitespace character, including spaces and tabs |
\\w |
A letter, digit, or underscore (and some other Unicode word characters) |
[^abc] |
Any character except a,
b, or c |
A\|B |
Either pattern A or pattern B |
( ) |
Group parts of a pattern together |
\\b |
A word boundary |
Notice that a caret (^) behaves differently inside
square brackets. At the beginning of a character class, it means
not rather than anchoring to the start of a string.
In R, the backslash is itself a special character in quoted strings.
As a result, we need to write two backslashes in our R
code to pass one backslash to the regular expression. For example,
"\\d" is how we tell regex to match a digit. Similarly,
"\\." matches an actual period, rather than using the
period as a wildcard.
We can also match a pattern without worrying about capitalization:
str_detect(c("Data", "DATA", "data", "date"),
regex("data", ignore_case = TRUE))
## [1] TRUE TRUE TRUE FALSE
Word boundaries are useful when we want to match an entire word rather than part of another word. For example:
words = c("cat", "cats", "the cat sleeps", "catalog", "Cat")
str_detect(words, "cat")
## [1] TRUE TRUE TRUE TRUE FALSE
str_detect(words, "\\bcat\\b")
## [1] TRUE FALSE TRUE FALSE FALSE
Finally, parentheses can group patterns, and | allows
alternatives:
str_detect(c("gray", "grey", "green", "GRay"), "gr(a|e)y")
## [1] TRUE TRUE FALSE FALSE
The expression (a|e) matches either "a" or
"e" in that position.
We have already used str_detect() to determine whether a
pattern occurs in a string. The str_extract() function
returns the matching portion instead, and str_extract_all()
retrieves every match when there are several.
Consider the task of extracting 10-digit phone numbers from text strings:
phone_strings = c("Home: 507-645-5489",
"Cell: 219.917.9871",
"My work phone is 507-202-2332",
"I don't have a phone")
A valid US area code has three digits and begins with a digit from 2
through 9. We can represent this as [2-9]\\d{2}. The
remaining digits are separated by periods or dashes, giving us the
following expression:
phone_pattern = "[2-9]\\d{2}[-.]\\d{3}[-.]\\d{4}"
str_detect(phone_strings, phone_pattern)
## [1] TRUE TRUE TRUE FALSE
str_extract(phone_strings, phone_pattern)
## [1] "507-645-5489" "219.917.9871" "507-202-2332" NA
We can also use str_replace() to remove identifying
information from the text:
str_replace(phone_strings, phone_pattern, "XXX-XXX-XXXX")
## [1] "Home: XXX-XXX-XXXX" "Cell: XXX-XXX-XXXX"
## [3] "My work phone is XXX-XXX-XXXX" "I don't have a phone"
Question #7: Consider the following vector containing phone numbers recorded using different separators:
phone_strings2 = c("Home: 507-645-5489",
"Cell: 219.917.9871",
"Work: 507-202-2332",
"Emergency: 612.555.0198",
"No phone available")
Part A: Use str_extract() and the
phone_pattern expression defined above to extract the phone
numbers from each string.
Part B: Standardize the extracted phone numbers so
that they all use dashes instead of periods (for example,
"219-917-9871"). Preserve NA for strings
without phone numbers.
In some circumstances, we may want to identify or remove text
enclosed in brackets, such as [8], (note), or
an HTML tag such as <tag>. These can be trickier than
they initially appear because regular expressions may match more text
than intended.
Consider the following strings:
out = c("abc[8]", "abc[9][20]", "abc[9]def[10][7]", "abc[]")
str_extract_all(out, "\\[.*\\]")
## [[1]]
## [1] "[8]"
##
## [[2]]
## [1] "[9][20]"
##
## [[3]]
## [1] "[9]def[10][7]"
##
## [[4]]
## [1] "[]"
str_extract_all(out, "\\[.*?\\]")
## [[1]]
## [1] "[8]"
##
## [[2]]
## [1] "[9]" "[20]"
##
## [[3]]
## [1] "[9]" "[10]" "[7]"
##
## [[4]]
## [1] "[]"
The first expression uses .*, which matches as many
characters as possible. Consequently, it can match everything between
the first opening bracket and last closing bracket.
This is called greedy matching. Adding ? after
.* makes the expression “lazy”, meaning it matches as few
characters as possible. This allows us to identify each bracketed
annotation separately.
Question #8: Suppose a collection of survey responses contains editorial notes enclosed in square brackets:
comments = c("The event was great [verified]",
"I enjoyed the food [staff note] but not the music [follow-up]",
"No complaints",
"The room was cold [temperature issue] and noisy [sound issue]")
Part A: Use str_count() to determine
how many bracketed annotations appear in each response.
Part B: Remove all bracketed annotations, including the brackets themselves, and eliminate any extra spaces left behind. Store the cleaned responses in a new vector.
Dates and times frequently appear in datasets, but they are often initially stored as character strings. R has special data types for dates and times that make calculations much easier.
For example, we can retrieve today’s date using
Sys.Date():
todays_date = Sys.Date()
todays_date
## [1] "2026-10-30"
class(todays_date)
## [1] "Date"
Although the output looks like a string, it is actually an object of
class "Date". We can subtract two dates to determine the
number of days between them:
todays_date - as.Date("2019-12-12")
## Time difference of 2514 days
However, dates in raw datasets can be written in many different ways.
For example, May 12, 2017 might appear as "May 12, 2017",
"5/12/2017", or "2017-05-12".
The lubridate package provides convenient functions for
converting these strings into dates. The function names describe the
expected order of the components:
| Function | Expected order |
|---|---|
mdy() |
Month, day, year |
ymd() |
Year, month, day |
dmy() |
Day, month, year |
library(lubridate)
mdy("May 12, 2017")
## [1] "2017-05-12"
mdy("5/12/2017")
## [1] "2017-05-12"
ymd("2017-05-12")
## [1] "2017-05-12"
dmy("12 May 2017")
## [1] "2017-05-12"
Once we have a date object, we can extract its components using
year(), month(), and day(). Other
useful functions include wday() for the day of the week and
yday() for the day of the year:
date1 = mdy("05/24/2017")
year(date1)
## [1] 2017
month(date1)
## [1] 5
day(date1)
## [1] 24
wday(date1, label = TRUE)
## [1] Wed
## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
yday(date1)
## [1] 144
Dates can also be rounded to a particular unit. For example,
floor_date() rounds down to the beginning of a month, which
can be useful when summarizing observations by month:
floor_date(date1, unit = "month")
## [1] "2017-05-01"
Some datasets also record a time of day. Functions
such as mdy_hm() and ymd_hms() can parse both
a date and a time. These produce date-time objects rather than ordinary
dates:
meeting = mdy_hm("May 12, 2017 4:45pm", tz = "America/Chicago")
meeting
## [1] "2017-05-12 16:45:00 CDT"
class(meeting)
## [1] "POSIXct" "POSIXt"
hour(meeting)
## [1] 16
minute(meeting)
## [1] 45
The tz argument specifies the time zone. We won’t
explore time zones in detail here, but they matter whenever observations
are recorded in different locations.
Question #9: On January 27, 1967, a cabin fire during a launch simulation killed the three astronauts aboard Apollo 1. Nearly 19 years later, on January 28, 1986, the Space Shuttle Challenger broke apart shortly after launch, killing all seven crew members.
Part A: Convert the following character strings into date objects and calculate how many days passed between the two events.
apollo = "1967 Jan 27"
challenger = "28 January 1986"
Part B: Determine the day of the week on which each event occurred.
Question #10: Consider the following dates associated with important events in the history of space exploration:
space_events = data.frame(
Event = c("Sputnik 1", "Explorer 1", "Vostok 1", "Apollo 1",
"Apollo 11", "Salyut 1", "Voyager 1", "STS-1",
"Challenger", "Hubble Telescope", "ISS Assembly",
"Columbia", "Curiosity", "Perseverance"),
Date = c("October 4, 1957", "January 31, 1958", "April 12, 1961",
"January 27, 1967", "July 20, 1969", "April 19, 1971",
"September 5, 1977", "April 12, 1981", "January 28, 1986",
"April 24, 1990", "November 20, 1998", "February 1, 2003",
"August 6, 2012", "February 18, 2021")
)
Part A: Convert the Date variable into
date objects using lubridate. Then create variables
containing the year, month, and weekday of each event.
Part B: As a sanity check that your date conversions worked correctly, calculate the number of days between the Apollo 1 fire and the Challenger disaster using the dates in your modified dataset.
Part C: Determine which month of the year appears most frequently in this dataset. Report the number of events occurring in each month, ordered from most to least frequent.
Part D: Identify which events occurred on a Monday. Return their names and dates.
The following problems require you to combine several of the string-processing tools from this lab. You may need to look back at earlier examples or consult the help documentation for particular functions.
Question #11: A small organization stores event registration information in text messages. Each record contains a registration code, a date, and an attendance status, but these are embedded in longer strings:
registrations = c(
"Registration EV-104: 2026-10-05 | Status: Confirmed",
"Registration EV-208: 2026-10-12 | Status: Cancelled",
"Registration EV-317: 2026-11-02 | Status: Confirmed",
"Registration EV-421: 2026-11-14 | Status: Pending",
"Registration EV-509: 2026-12-01 | Status: Confirmed",
"Registration EV-612: 2026-12-08 | Status: Cancelled",
"Registration EV-715: 2026-10-18 | Status: Pending",
"Registration EV-823: 2026-11-06 | Status: Confirmed",
"Registration EV-936: 2026-11-21 | Status: Cancelled",
"Registration EV-147: 2026-12-04 | Status: Pending",
"Registration EV-258: 2026-10-27 | Status: Confirmed",
"Registration EV-369: 2026-11-11 | Status: Confirmed",
"Registration EV-471: 2026-12-15 | Status: Confirmed",
"Registration EV-582: 2026-11-25 | Status: Pending",
"Registration EV-693: 2026-10-09 | Status: Cancelled",
"Registration EV-704: 2026-11-19 | Status: Confirmed",
"Registration EV-815: 2026-12-22 | Status: Cancelled",
"Registration EV-926: 2026-10-31 | Status: Confirmed",
"Registration EV-138: 2026-11-30 | Status: Confirmed",
"Registration EV-249: 2026-12-12 | Status: Pending"
)
Part A: Extract the registration codes (such as
"EV-104") and dates from each string. Convert the dates
into date objects.
Part B: Extract the attendance status from each record. Use your results to construct a data frame containing the registration code, date, and status in separate columns.
Part C: Identify the registrations that are confirmed and scheduled during November 2026. Return their registration codes.