Difference between grep() and grepl()
Most of the time these two functions are considered the same. Though both the functions are used to check whether a particular pattern matches in the given collection of strings but they differ in the types of output returned by them.
- grep(): This function returns a vector of indices of the character strings that contain the pattern.
- grepl(): This function returns TRUE if a pattern exists in a character string.
Example:
In this example, we are searching the pattern “w3wiki” in the data vector using grep() function, it returns 1 since this pattern is located at the index 1 in the given vector. Also, we are searching the pattern “Bhuwanesh” in the same vector but using grepl() function this time, and it returns a set of boolean values describing whether the ith element of the vector contains this pattern or not.
R
# create a vector of data data <- c ( "w3wiki" , "gfg" , "Bhuwanesh" , "Nainwal" , "Swift" ) grep ( "w3wiki" , data) grepl ( "Bhuwanesh" , data) |
Output:
Difference Between grep() vs. grepl() in R
In this article, we will discuss the difference between grep() and grepl() in R programming language.
Contact Us