Overview
The word “grepl” stands for “grep logical”. The grepl()
function in R simply searches for matches in characters or sequences of characters present in a given string.
Syntax
The syntax for the grepl
method is as follows:
grepl(pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
Parameters
pattern
: The character or sequence of characters that will be matched against the specified elements of the string.x
: The specified string vector.ignore.case
: IfTRUE
i.e., it finds a match, the code ignores the upper or lowercase. This is optional.perl
: This is a logical value. To know if perl-compatible regexps should be used or to know if the priority has been overextended. This is optional.fixed
: This is a logical value. IfTRUE
, then the pattern of the characters or sequence of characters is matched. This is optional.useBytes
: This is a logical value. IfTRUE
, the matching is simply done byte-by-byte instead of character-by-character. This makes the program faster and is optional.
Return value
The grepl()
function returns FALSE
or TRUE
for matches in characters or a sequence of characters present in a string.
Example 1
# Creating string vector
x <- c("CAR", "BIKE")
See AlsoHow to Search Files Effectively in the Linux Terminalgrepl() in R | Hyperskill UniversityUse the Linux grep CommandAI Coding Assistant for Beginners# Calling grepL() function
grepl("CA", x)
From the output of the code:
[1] TRUE FALSE
We can see that it returns TRUE
. This means that CA
exists in the first item of the string variable CAR
. FALSE
means it is absent in the second item of the string variable BIKE
.
Example 2
# creating a string variable
name <- c("CAR", "bIKE", "BICYCLE", "AEROPLANE")
# passing ignore.case argument to the grepl() function
grepl("bi", name, ignore.case = TRUE)
From the output of the code above:
[1] FALSE TRUE TRUE FALSE
We can see that it returns TRUE
and TRUE
for the second and third elements of the string variable, "bIKE"
and "BICYCLE"
. This happens even though they are not all in the lower case like the argument we pass to the grepl()
function. This way, the ignore.case
parameter makes R ignore the cases of the texts.
Example 3
# creating a string variable
name <- c("CAR", "b|ke", "BICYCLE", "AEROPLANE")
grepl("b.", name, fixed = FALSE, perl=FALSE)
grepl("b.", name, fixed = TRUE, perl=FALSE)
From the output of the code with fixed = FALSE
:
[1] FALSE TRUE FALSE FALSE
In a regular expression, the .
metacharacter matches any single character. Therefore, the pattern b.
will match any string in the name
vector that has a b
followed by any single character. As a result, it will return TRUE
for element b|ke
since it contains a b
followed by another character.
From the output of the code with fixed = TRUE
:
[1] FALSE FALSE FALSE FALSE
With fixed = TRUE
, the .
is interpreted literally, so it will only match the exact string b.
. Since none of the elements in the name vector contain b.
, the function will return FALSE
for all elements.