📖 Scenario: Imagine you have a list of fruit names and you want to find which fruits contain the word "apple".
🎯 Goal: You will learn how to use grep and grepl functions in R to search for words inside a list of strings.
📋 What You'll Learn
Create a character vector with fruit names
Create a search word variable
Use grep to find positions of fruits containing the search word
Use grepl to get a logical vector showing which fruits contain the search word
Print the results
💡 Why This Matters
🌍 Real World
Searching text data for specific words or patterns is common in data analysis, such as finding keywords in customer feedback or filtering product names.
💼 Career
Knowing how to use <code>grep</code> and <code>grepl</code> helps in data cleaning, text mining, and preparing data for reports or machine learning.
Progress0 / 4 steps
1
Create a vector of fruit names
Create a character vector called fruits with these exact values: "apple", "banana", "pineapple", "orange", "grape"
R Programming
Hint
Use the c() function to combine the fruit names into a vector.
2
Create a search word variable
Create a variable called search_word and set it to the string "apple"
R Programming
Hint
Just assign the string "apple" to the variable search_word.
3
Use grep and grepl to find fruits containing the search word
Use grep with search_word and fruits to find the positions of fruits containing "apple" and store it in positions. Then use grepl with the same inputs to get a logical vector and store it in matches.
R Programming
Hint
Remember grep returns positions (numbers), and grepl returns TRUE or FALSE for each element.
4
Print the results
Print the positions vector and then print the matches vector to show which fruits contain the word "apple".
R Programming
Hint
Use print() to show the vectors.
Practice
(1/5)
1. What does the grepl function in R return when searching for a pattern in a character vector?
easy
A. The matching elements themselves
B. The positions of elements matching the pattern
C. A logical vector indicating TRUE or FALSE for each element
D. The count of matches found
Solution
Step 1: Understand the purpose of grepl
grepl checks each element of a vector and returns TRUE if the pattern is found, otherwise FALSE.
Step 2: Compare with other functions
grep returns positions or values, but grepl returns logical TRUE/FALSE values for each element.
Final Answer:
A logical vector indicating TRUE or FALSE for each element -> Option C
2. Which of the following is the correct syntax to find the positions of elements containing the word "apple" in vector fruits using grep ignoring case?
easy
A. grep("apple", fruits, ignore.case = TRUE)
B. grepl("apple", fruits, ignore.case = TRUE)
C. grep(fruits, "apple", ignore.case = TRUE)
D. grepl(fruits, "apple", ignore.case = TRUE)
Solution
Step 1: Identify correct argument order for grep
The first argument is the pattern, second is the vector to search.
Step 2: Check function name and parameters
grep("apple", fruits, ignore.case = TRUE) correctly searches for "apple" ignoring case and returns positions.
Final Answer:
grep("apple", fruits, ignore.case = TRUE) -> Option A
Quick Check:
Pattern first, vector second in grep [OK]
Hint: Pattern goes first, vector second in grep [OK]
Hint: grep returns positions, not values or logicals [OK]
Common Mistakes:
Expecting matching values instead of positions
Confusing grep with grepl output
Misreading output format with index labels
4. The following code is intended to return a logical vector indicating which elements contain "blue" ignoring case, but it gives an error. What is the error?
B. The pattern and vector arguments are swapped in grepl
C. colors vector contains uppercase letters causing error
D. grepl cannot be used with character vectors
Solution
Step 1: Check argument order for grepl
grepl expects pattern first, then the vector to search.
Step 2: Identify swapped arguments in code
The code uses grepl(colors, "blue", ignore.case = TRUE) which swaps pattern and vector.
Final Answer:
The pattern and vector arguments are swapped in grepl -> Option B
Quick Check:
Pattern first, vector second in grepl [OK]
Hint: Pattern always comes first in grepl [OK]
Common Mistakes:
Swapping pattern and vector arguments
Thinking ignore.case is invalid
Assuming case in vector causes error
5. You have a vector names <- c("Anna", "Annie", "Annette", "Bob", "An"). You want to find all names that start with "Ann" ignoring case. Which code correctly returns the matching names?
hard
A. names[grepl("^Ann", names, ignore.case = TRUE)]
B. grep("Ann", names, ignore.case = TRUE)
C. names[grep("Ann", names)]
D. grepl(names, "^Ann", ignore.case = TRUE)
Solution
Step 1: Use grepl with pattern and vector correctly
To filter names starting with "Ann", use grepl("^Ann", names, ignore.case = TRUE) which returns TRUE for matching elements.
Step 2: Subset names vector using logical vector
Use names[logical_vector] to get matching names.
Final Answer:
names[grepl("^Ann", names, ignore.case = TRUE)] -> Option A
Quick Check:
grepl + logical indexing filters names [OK]
Hint: Use grepl with ^ for start and subset vector [OK]
Common Mistakes:
Using grep without subsetting vector
Swapping pattern and vector in grepl
Not using ^ to match start of string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.