The grepl() function in R is used to find a pattern inside a vector. If you are new to this function and want to know more about it, please check out our instructions below because we will discuss more details about the functions and how to use them in our program.
What does the grepl() function do in R?
To understand the grepl() function, we should first know about the grep() function.
The grep() function is a built-in function that checks if a pattern exists in a vector. If yes, it will return a vector that contains the position of the matched elements.
Syntax:
grep(pattern, vector)
Parameters:
- pattern: the pattern that you want to check inside the vector.
- vector: the given/specified vector.
Return value: a vector that contains the position of the matched elements.
Example:
websites <- c("w3schools.com", "learnshareit.com", "learnshareit.com", "stackoverflow.com", "freecodecamp.com", "learnshareit.com", "coursera.org", "learnshareit.com", "google.com") grep("learnshareit.com", websites)
Output:
[1] 2 3 6 8
Let’s get back to the grepl() function. The “l” in “grepl” stands for “logical”. The grepl() function also checks if a pattern exists in a vector or not, but it will return True or False while checking. So, the difference between the grep() and grepl() function is that the grep() will return a vector with the positions of the matched elements, while the grepl() function will return a vector that contains only True – if matched, and False – if not matched values.
How to use grepl() in R?
Syntax:
grepl(pattern, vector)
Parameters:
- pattern: the pattern that you want to check inside the vector.
- vector: the given/specified vector.
Return value: a logical vector. True if the value matches and False if it doesn’t.
Example:
websites <- c("w3schools.com", "learnshareit.com", "learnshareit.com", "stackoverflow.com", "freecodecamp.com", "learnshareit.com", "coursera.org", "learnshareit.com", "google.com") grepl("learnshareit.com", websites)
Output:
[1] FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE
We can add some arguments to give the function some more options. For example, ‘ignore.case
‘. If it is set to True, then it will ignore the case. If it is set to False, the pattern will be case sensitive.
Example:
names <- c("Alex", "John", "bob", "john", "Tommy", "kate", "jOhn", "Noah") grepl("john", names, ignore.case = TRUE)
Output:
[1] FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
Summary
This tutorial shows you the grepl() function in R and how to apply them in our program. The grep() function will check whether a pattern is inside a vector. If a value is matched, then it will return True. Otherwise False.
Maybe you are interested:
- The mdy() Function In R
- assign() Function In R: Assign Values To Variables
- Train Function In R: Fit Predict Model

Hello. My name is Khanh Hai Ngo. I graduated in Information Technology at VinUni. My advanced programming languages include C, C++, Python, Java, JavaScript, TypeScript, and R, which I would like to share with you. You will benefit from my content.
Name of the university: VinUni
Major: EE
Programming Languages: C, C++, Python, Java, JavaScript, TypeScript, R