📖 Scenario: You work in a small library. You have a list of book titles mixed with some extra text. You want to find which titles mention the word "data" in any form.
🎯 Goal: Build a simple R program that uses regular expressions to find and print book titles containing the word "data".
📋 What You'll Learn
Create a character vector called books with given titles
Create a regular expression pattern variable called pattern
Use grep() with pattern on books to find matching titles
Print the matching titles using print()
💡 Why This Matters
🌍 Real World
Libraries, bookstores, or data analysts often need to search text data for keywords quickly and accurately.
💼 Career
Knowing regular expressions and text search functions in R is useful for data cleaning, text mining, and report generation.
Progress0 / 4 steps
1
Create the book titles vector
Create a character vector called books with these exact entries: "Data Science Basics", "Introduction to R", "Advanced Data Analysis", "Cooking Recipes", "Big Data Trends"
R Programming
Hint
Use the c() function to create a vector of strings.
2
Create the regular expression pattern
Create a variable called pattern and set it to the regular expression string "data" to match the word "data" in any case.
R Programming
Hint
Just assign the string "data" to the variable pattern.
3
Find titles matching the pattern
Use grep() with pattern on books to find indices of titles containing "data" ignoring case. Store the result in a variable called matches.
R Programming
Hint
Use grep(pattern, books, ignore.case = TRUE) to find matches ignoring case.
4
Print the matching book titles
Use print() to display the titles from books at the positions stored in matches.
R Programming
Hint
Use print(books[matches]) to show the matching titles.
Practice
(1/5)
1. What does the grepl() function do in R when used with regular expressions?
easy
A. It checks if a pattern exists in a string and returns TRUE or FALSE.
B. It replaces all occurrences of a pattern in a string.
C. It extracts all matches of a pattern from a string.
D. It splits a string into parts based on a pattern.
Solution
Step 1: Understand the purpose of grepl()
grepl() returns a logical value indicating if the pattern is found in the string.
Step 2: Compare with other functions
Functions like gsub() replace text, and strsplit() splits strings, so they do not match grepl()'s behavior.
Final Answer:
It checks if a pattern exists in a string and returns TRUE or FALSE. -> Option A
Quick Check:
grepl() = logical check [OK]
Hint: Remember: grepl() returns TRUE/FALSE if pattern found [OK]
Common Mistakes:
Confusing grepl() with gsub() which replaces text
Thinking grepl() extracts matched text
Assuming grepl() splits strings
2. Which of the following is the correct syntax to replace all occurrences of "cat" with "dog" in the string "cat and cat" using regular expressions in R?
easy
A. sub("cat", "dog", "cat and cat")
B. str_replace_all("cat and cat", "cat", "dog")
C. gsub("cat", "dog", "cat and cat")
D. replace("cat", "dog", "cat and cat")
Solution
Step 1: Identify function for replacing all occurrences
gsub() replaces all matches, while sub() replaces only the first.
Step 2: Check syntax correctness
gsub(pattern, replacement, string) is the correct order and usage in R.
Final Answer:
gsub("cat", "dog", "cat and cat") -> Option C
Quick Check:
Replace all = gsub() [OK]
Hint: Use gsub() to replace all matches, sub() only first [OK]
Which regular expression pattern would correctly identify emails ending with ".com" using grepl()?
hard
A. "\\.com$"
B. ".com"
C. "com$"
D. "\\.com"
Solution
Step 1: Understand the pattern requirements
We want to match emails ending exactly with ".com". The dot must be escaped as "\\." to match a literal dot, and "$" means end of string.
Step 2: Analyze each option
"\\.com$" uses "\\.com$" which matches '.com' at the end. ".com" matches any character before 'com', not necessarily end. "com$" misses the dot. "\\.com" matches '.com' anywhere, not necessarily at end.
Final Answer:
"\\.com$" -> Option A
Quick Check:
Escape dot and use $ for end match [OK]
Hint: Escape dot with \\. and use $ to match string end [OK]
Common Mistakes:
Not escaping dot, so matches any character
Missing $ causing partial matches
Confusing string end anchor with start anchor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.