Check for a Pattern in the Vector in R Programming – grepl() Function

grepl() function in R Language is used to return the value True if the specified pattern is found in the vector and false if it is not found.

Syntax: grepl(pattern, string, ignore.case=FALSE)

Parameters:
pattern: regular expressions pattern
string: character vector to be searched
ignore.case: whether to ignore case in the search. Here ignore.case is an optional parameter as is set to FALSE by default.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# grepl function
  
# Initializing a character vector
str <- c("GFG", "gfg", "Geek", "Geeks"
  
# Calling the grepl() function to
# find whether any instance(s) of
# ‘GF’ and 'G' are present in the string
grepl('GF', str, ignore.case ="True"
grepl('G', str, ignore.case ="True"

chevron_right


Output:

[1]  TRUE  TRUE FALSE FALSE
[1] TRUE TRUE TRUE TRUE

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# grepl function
  
# Initializing a character vector
str <- c("GFG", "gfg", "Geek", "Geeks"
  
# Calling the grepl() function to
# find whether any instance(s) of
# ‘gfg’ and 'Geek' are present in the string
grepl('gfg', str
grepl('Geek', str

chevron_right


Output:

[1] FALSE  TRUE FALSE FALSE
[1] FALSE FALSE  TRUE  TRUE
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.