Open In App

Matching of patterns in a String in R Programming - agrep() Function

Last Updated : 14 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Syntax: 
agrep(pattern, x, ignore.case=FALSE, value=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 ignore case. 
value: If its value is TRUE, it return the matching elements vector, else return the indices vector. 
 


Example 1: 
 

Python3
# R program to illustrate
# agrep function

# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")

# Calling agrep() function
agrep("gfg", x)
agrep("Geeks", x)
agrep("gfg", x, ignore.case = TRUE)
agrep("Geeks", x, ignore.case = TRUE)

Output : 
 

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


Example 2: 
 

Python3
# R program to illustrate
# agrep function

# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")

# Calling agrep() function
agrep("gfg", x, ignore.case = TRUE, value = TRUE)
agrep("G", x, ignore.case = TRUE, value = TRUE)
agrep("Geeks", x, ignore.case = FALSE, value = FALSE)
agrep("GEEKS", x, ignore.case = FALSE, value = FALSE)           

Output: 
 

[1] "GFG" "gfg"
[1] "GFG"   "gfg"   "Geeks" "GEEKS"
[1] 3
[1] 4


 


Next Article

Similar Reads