Exams Knowledge Hub

MCQs for Competitive Exams, School & College Exams

Module: | Base R Data Structures & Subsetting

Q6: Consider the following statements regarding pattern matching functions in R:

1. The default behavior of the base grep() function is to return the integer indices of the elements within a character vector that match a specified regular expression pattern.
2. Utilizing the argument value = TRUE within grep() instructs the engine to return the actual matching character strings instead of their index positions.
3. The R 4.4.0 release officially introduced a new base function named grepv(), which acts as a direct wrapper for grep() but automatically defaults to value = TRUE.

Which of the above statements is/are correct?
A
Only 1 and 2
B
Only 2 and 3
C
Only 1 and 3
D
1, 2, and 3
✅ Correct Answer: D
🎯 Quick Answer:
D. 1, 2, and 3 are all correct statements.
Concept Definition: Pattern matching in R is handled by a family of functions (grep, grepl, sub, gsub) that leverage POSIX or PCRE regular expressions to search through character vectors.
Structural Breakdown: Calling grep(pattern, x) identifies the exact location of a match in a vector.
Modifying the call to grep(pattern, x, value = TRUE) extracts the actual string data.
Historical/Related Context: For over two decades, R developers had to manually type value = TRUE to extract strings, representing a highly frequent operation in data wrangling and text mining scripts.
Causal Reasoning: To heavily streamline character vector manipulation, the R Core Team introduced grepv() in R 4.4.0 (April 2024). This syntactic sugar reduces standard boilerplate code, directly improving the quality of life for analysts extracting text directly from string arrays.