Challenge - 5 Problems
grep and grepl Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of grep with value = TRUE
What is the output of this R code?
vec <- c("apple", "banana", "cherry", "date")
grep("a", vec, value = TRUE)R Programming
vec <- c("apple", "banana", "cherry", "date") grep("a", vec, value = TRUE)
Attempts:
2 left
💡 Hint
Remember, value = TRUE returns the matching elements, not their positions.
✗ Incorrect
grep with value = TRUE returns the elements of the vector that contain the pattern. Here, "apple", "banana", and "date" contain the letter 'a'.
❓ Predict Output
intermediate2:00remaining
Output of grepl with logical vector
What is the output of this R code?
vec <- c("cat", "dog", "bird", "cow")
grepl("o", vec)R Programming
vec <- c("cat", "dog", "bird", "cow") grepl("o", vec)
Attempts:
2 left
💡 Hint
grepl returns TRUE for elements containing the pattern, FALSE otherwise.
✗ Incorrect
Only "dog" and "cow" contain the letter 'o', so their positions are TRUE, others FALSE.
🧠 Conceptual
advanced2:00remaining
Difference between grep and grepl
Which statement correctly describes the difference between grep() and grepl() in R?
Attempts:
2 left
💡 Hint
Think about what each function returns: positions vs logical flags.
✗ Incorrect
grep() returns the indices or matching values (if value=TRUE), while grepl() returns a logical vector indicating which elements matched.
❓ Predict Output
advanced2:00remaining
Output of grep with fixed = TRUE
What is the output of this R code?
vec <- c("a.c", "abc", "a-c", "a.c")
grep("a.c", vec, fixed = TRUE)R Programming
vec <- c("a.c", "abc", "a-c", "a.c") grep("a.c", vec, fixed = TRUE)
Attempts:
2 left
💡 Hint
fixed = TRUE treats the pattern as a plain string, not a regular expression.
✗ Incorrect
With fixed = TRUE, the pattern "a.c" matches exactly "a.c" strings at positions 1 and 4.
❓ Predict Output
expert2:00remaining
Count of matches using grepl and sum
What is the value of count after running this R code?
vec <- c("red", "green", "blue", "yellow", "redgreen")
count <- sum(grepl("red", vec))
countR Programming
vec <- c("red", "green", "blue", "yellow", "redgreen") count <- sum(grepl("red", vec)) count
Attempts:
2 left
💡 Hint
grepl returns TRUE for each element containing "red"; sum counts TRUE as 1.
✗ Incorrect
The elements "red" and "redgreen" contain "red", so count is 2.