grepl() Function in R

Learn via video courses
Topics Covered

Overview

When we work with large datasets or documents, extracting specific information and identifying patterns within character vectors is crucial in our data analysis process. It includes finding specific patterns or structures inside lengthier texts or data strings, such as words, phrases, or symbols. This method is essential for our activities like text processing, data extraction, and pattern recognition in various applications, ranging from web searching to data mining.

The R programming language's grepl() function, which allows us to detect and match patterns in lists of letters and symbols, will be discussed in this article.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Introduction to grepl() Function in R

The function grepl in R, which stands for "grep logical," is a basic built-in function in the R programming language. Its main role is to find matches inside a string or a string vector. When called, the grepl() function returns a logical vector indicating what elements in the provided vector have matched the specified search pattern.

Subsetting rows within an R data frame is a useful application of the grepl() function. It can be done using single-square brackets and supplying the column containing character values. We can also filter and extract the rows with the required character pattern using the grepl() function.

grep() and grepl() functions in R

It's important to note that there is another closely related function called grep() in R. While grepl() returns a logical vector indicating matches, grep() serves a similar purpose by searching for character pattern matches as well. However, it differs in output: instead of providing a logical vector, grep() returns the indices or positions of the matched elements within the vector. These functions are very useful in different scenarios, depending on whether we need the matching positions or a logical indication of matches.

Syntax and Parameters

To use the grepl() function in R, we can use the following syntax:

In this syntax of the grepl() function, the different parameters are:

  • pattern: The pattern we want to search for within the character vector.
  • x: The character vector or list of strings we want to search for the pattern.
  • ignore.case: A logical value indicating whether to perform a case-insensitive search. If it is not specified, the default value for the ignore.case parameter is set to FALSE.
  • perl: A logical value indicating whether to use Perl-compatible regular expressions. If it is not specified, the default value for the perl parameter is set to FALSE.
  • fixed: A logical value indicating whether to treat the pattern as a fixed string. If it is not specified, the default value for the fixed parameter is set to FALSE.
  • useBytes: A logical value indicating whether to use byte-based matching. If it is not specified, the default value for the useBytes parameter is set to FALSE.
Sharpen Your Fundamentals with Free Learning

Return Value

The grepl() function returns a logical vector of the same length as the input vector x, where each element corresponds to whether the pattern was found in the corresponding element of x. If there is a match, it will return TRUE and FALSE if there is no match.

Examples

Before we discuss the examples, let's create a simple data frame named "employee." In this dataset, we will have three columns: "age", "country", and "email." To create the "employee" data frame, we will use the following code:

Output:

We will use this data frame to explore different examples of pattern matching techniques in R.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example 1: Using grepl() function for string pattern

Let us use the following code to check the occurrence of the string "example.com":

Output:

Here, we used the grepl() function to check if the pattern example.com exists in the email column of the employee data frame. The output is a logical vector ex1, where TRUE indicates a match, and FALSE indicates no match.

We can even add a new column named ex1 to the employee data frame, containing the values from the ex1 logical vector using the following code:

Output:

Example 2: Using grep() function for string pattern

Instead of the grepl() function, let us use the grep() function to check the occurrence of the string "example.com", as shown below:

Output:

Here, we used the grep() function to search for the pattern "example.com" within the email column of the employee data frame. The output returns the indices of matching elements.

Also, we can create a new data frame named "df_ex2" that contains only the rows of employees whose email addresses contain "example.com", as shown below:

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 3: Using grepl() function for string pattern without considering the text case

Let us check the occurrences of the string "Germany" in the country column using the following code:

Output:

Here, we used the grepl() function to check if the string "Germany" exists in the country column of the employee data frame. The result is a logical vector that indicates whether each element of the country column contains the string 'Germany' with TRUE indicating a match and FALSE indicating no match.

Example 4: Using grepl() function with ignore.case argument

Let us check the occurrences of the string "Germany" again in the country column using the following code:

Output:

Here, we used the grepl() function with the ignore.case argument to check if the string "Germany" exists in the country column of the employee data frame. The ignore.case argument, set to TRUE, performs a case-insensitive search for the string "Germany" in the country column. Then, it prints TRUE or FALSE values indicating whether it contains the string "Germany" by ignoring the text case.

Example 5: Using grepl() function with perl argument

Let us check the occurrences of two consecutive digits in the age column as shown below:

Output:

We used the grepl() function with the perl argument to search for a regular expression pattern. The perl argument, set to TRUE, checks if there are two consecutive digits in the age column of the employee data frame. The output is a logical vector ex5, where TRUE indicates a match, and FALSE indicates no match.

Example 6: Using grepl() function with fixed argument

Let us search for the exact string in the country column as shown below:

Output:

Here we used the grepl() function with the fixed argument to search for the exact string "norway" in the country column of the employee data frame. The output is a logical vector ex6 which displays TRUE or FALSE values, indicating whether there is an exact case-sensitive match with "norway."

Conclusion

In conclusion:

  • The function grepl in R allows us to find and manipulate data based on specific patterns within text.
  • It can be used with other functions like grep() to achieve various pattern matching tasks, providing flexibility for different use cases.
  • The function grepl in R provides different optional arguments like ignore.case, perl, and fixed, each serving a unique purpose.