grep()
This grep() function in R Language allows programmers to search for a match of a particular pattern in the given collection of strings. The syntax is given below,
Syntax: grep(stringPattern, x, ignore.case=TRUE or FALSE, value=TRUE or FALSE)
Parameters:
- stringPattern: A pattern that has 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:
R
# R program to illustrate # grep function # Initializing a string vector x <- c ( "w3wiki" , "Bhuwanesh" , "Nainwal" , "gfg" ) # Calling grep() function grep ( "w3wiki" , x) grep ( "Bhuwanesh" , x) grep ( "gfg" , x, ignore.case = FALSE ) grep ( "Nainwal" , x, ignore.case = TRUE ) |
Output:
Example 2:
R
# R program to illustrate # grep function # Creating string vector x <- c ( "w3wiki" , "Bhuwanesh" , "Nainwal" , "gfg" ) # Calling grep() function grep ( "gfg" , x, ignore.case = TRUE , value = TRUE ) grep ( "Bhuwanesh" , x, ignore.case = TRUE , value = TRUE ) grep ( "w3wiki" , x, ignore.case = FALSE , value = FALSE ) grep ( "Nainwal" , x, ignore.case = FALSE , value = FALSE ) |
Output:
Difference Between grep() vs. grepl() in R
In this article, we will discuss the difference between grep() and grepl() in R programming language.