grepl in R: How to Use R grepl() Function

We often come across a functionality where we want to check something by comparing it with patten. We use a regular expression to match specific character vectors in the string. Let’s see how to use regular expression in R.

The functions like grep(), grepl(), regexpr(), gregexpr(), and regexec() search for matches to argument pattern within every item of a character vector. The sub() and gsub() functions execute the replacement of the first and all matches sequentially.

If you are using Rstudio and you want the documentation of the grepl() method, then type the following command.

?grepl

And you will find all the information about the grepl() method like the below image.

grepl in R with example

grepl in R

The grepl in R is a built-in function that searches for matches of a string or string vector. The grepl() method takes a pattern and returns TRUE if a string contains the pattern, otherwise FALSE. The grepl stands for “grep logical”.

If the parameter is a string vector, it returns a logical vector (match or not for each vector element).

To find a pattern in string or string vector, use the grepl() function. In their most basic form, regular expressions can see whether a pattern exists inside a character string or a vector of character strings.

Syntax

grepl(pattern, x, ignore.case = FALSE, perl = FALSE,
      fixed = FALSE, useBytes = FALSE)

Parameters

pattern: It is a regular expression or string for fixed=TRUE

x: It is a string, the character vector

perl: Logical. Should Perl-compatible regexps be used? Has priority been overextended?

fixed: It is logical. If the TRUE, the pattern is a string to be matched as is. Overrides all conflicting arguments

useBytes: It is logical. If TRUE, the matching is done byte-by-byte rather than character-by-character. If you pass useBytes = TRUE, the strings will not be checked before matching, and the real matching will be faster.

Both functions need a pattern and an x argument, where the pattern is the regular expression you want to match for, and the x argument is the character vector from which matches should be sought. If you are performing a lot of regular expression matching, including on very long strings, you might want to consider the options used.

Example 1

Let’s create a vector consists of strings.

rv <- c("KHUSHI", "KRUNAL", "MATE", "AUS")
grepl("K", rv)

Output

[1] TRUE TRUE FALSE FALSE

You can see that it returns a logical vector indicating that if a specific element is present or not. TRUE means it exists, and FALSE means do not exist. K exists in the first and second elements of the Vector, and that’s why it returns TRUE and otherwise FALSE.

Example 2

sv <- "Infosys is currently trading at 1435 Rs."

op <- grepl("\\d+", sv)

print(op)

Output

[1] TRUE

In this example, we are searching if the string vector contains a numeric value or not. It returns TRUE because it has a numeric value.

Passing ignore.case argument to the grepl() function

To ignore the cases of the letters while searching for patterns in R, use the grepl() function and pass the ignore.case = TRUE. It will ignore whether it is in capital or small letters in the string.

rv <- c("khushi", "KRUNAL", "MATE", "AUS")
grepl("K", rv, ignore.case = T)

Output

[1] TRUE TRUE FALSE FALSE

You can see that even though the first element has a small letter k in “khushi”, it still returns TRUE because we explicitly told the R compiler to ignore the case while searching.

If we pass ignore.case = F or FALSE, it will only return the TRUE on the second position.

rv <- c("khushi", "KRUNAL", "MATE", "AUS")
grepl("K", rv, ignore.case = F)

Output

[1] FALSE TRUE FALSE FALSE

grep in R

The grep() is a built-in R function that returns the index of the match. Take the above example and find the index of matched elements.

rv <- c("KHUSHI", "KRUNAL", "MATE", "AUS")
grep("K", rv)

Output

[1] 1  2

You can see that K exists in the 1st and 2nd items of the Vector; that’s why it returns the indices of that Vector, which are 1 and 2.

That is it for this tutorial.

Leave a Comment