Find position of a Matched Pattern in a String in R Programming – grep() Function

grep() function in R Language is used to search for matches of a pattern within each element of the given string.

Syntax:
grep(pattern, x, ignore.case=TRUE/FALSE, value=TRUE/FALSE)

Parameters:
pattern: Specified pattern which is going to be matched with given elements of the string.
x: Specified string vector.
ignore.case: If its value is TRUE, it ignores case.
value: If its value is TRUE, it return the matching elements vector, else return the indices vector.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# grep functiom
  
# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")
  
# Calling grep() function
grep("gfg", x)
grep("Geeks", x)
grep("gfg", x, ignore.case = FALSE)
grep("Geeks", x, ignore.case = TRUE)

chevron_right


Output :

[1] 2
[1] 3
[1] 2
[1] 3 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# grep functiom
  
# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")
  
# Calling grep() function
grep("gfg", x, ignore.case = TRUE, value = TRUE)
grep("G", x, ignore.case = TRUE, value = TRUE)
grep("Geeks", x, ignore.case = FALSE, value = FALSE)
grep("GEEKS", x, ignore.case = FALSE, value = FALSE)           

chevron_right


Output:

[1] "GFG" "gfg"
[1] "GFG"   "gfg"   "Geeks" "GEEKS"
[1] 3
[1] 4
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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.