In this article, we will show you what the grep()
function in R does and how to use it in R. The grep()
function in R is used to find the position of the vector that is matching with the pattern. Let’s learn more about the grep()
function with the explanation and examples below.
Grep Function In R
What does the grep function do in R?
I think most of you learned one of the high-level languages, such as: Java, Python, … before learning R. These languages also have an important part when working with the String is the regular expression to split or find the String, which matches with the pattern. R also has some functions which are the same function as them. And the grep()
function is one of them, which is used to find the position of the elements in the vector which match the specified pattern. Let’s take a look at its syntax.
Syntax:
grep(pattern, x, ignore.case, perl, value, fixed, useBytes, invert)
Parameters:
- pattern: The regular expression.
- x: The vector to find the elements that match the pattern.
- ignore.case: The default is FALSE. Case insensitive or not.
- perl: The default is FALSE. Use the Perl-compatible regexps or not.
- value: The default is FALSE. If TRUE, return the value of elements that match the pattern. Otherwise, return the position.
- fixed: The default is FALSE. The pattern is a string that is matched as-is or not.
- useBytes: The default is FALSE. Use the byte-by-byte matching or not.
- invert: The default is FALSE. If true, return the elements which do not match the pattern.
After learning the definition and the syntax of the grep()
function, you will learn how to use it in the next title below.
How to use the grep function in R?
Find the position of elements matching the pattern in the vector
You can find the position of elements matching the pattern in the vector by the grep()
function.
Look at the example below.
# Create the character vector vec <- c("Google", "Facebook", "Youtube", "Tik Tok", "W3Schools", "GeeksforGeeks", "LearnShareIT") # Find the position of elements that have 's' grep("s", vec)
Output
[1] 5 6
Find the elements matching the pattern in the vector
You can find the value of elements matching the pattern in the vector by the grep() function by assigning the parameter named ‘value’ as TRUE.
Look at the example below.
# Create the character vector vec <- c("Google", "Facebook", "Youtube", "Tiktok", "W3Schools", "GeeksforGeeks", "LearnShareIT") # Find the elements which have 'r' grep("r", vec, value = TRUE)
Output
[1] "GeeksforGeeks" "LearnShareIT"
Find the elements with multiple patterns
You can find the elements with multiple patterns by the grep()
function.
Look at the example below.
# Create the character vector vec <- c("Google", "Facebook", "Youtube", "Tik Tok", "W3Schools", "GeeksforGeeks", "LearnShareIT") # Find the elements which have 'eb', 'ea' or 'ee' grep("eb|ea|ee", vec, ignore.case = TRUE, value = TRUE)
Output
[1] "Facebook" "GeeksforGeeks" "LearnShareIT"
You can learn how to merge two data frames by row names in R here.
Summary
You have learned the definition, the syntax, and how to use the grep
function in R. The grep()
function in R is one of the functions using the regular expression to work with the String. By this function, you can find the position of the value of the elements matching the pattern in the vector. We hope this tutorial is helpful to you. Thanks!
Maybe you are interested:
- vector function in r. Create, convert or check if the object is the vector
- The as.factor() function in R
- The cor.test() function in R

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R