Filtering row which contains a certain string using Dplyr in R - GeeksforGeeks

28 Jul, 2021
In this article, we will learn how to filter rows that contain a certain string using dplyr package in R programming language.
Two main functions which will be used to carry out this task are:
filter(): dplyr package’s filter function will be used for filtering rows based on condition
Syntax: filter(df , condition)
Parameter :
df:  The data frame object
condition:  The condition to filter the data upon
grepl(): grepl() function will is used to return the value TRUE if the specified string pattern is found in the vector and FALSE if it is not found.
Syntax: grepl(pattern, string, ignore.case=FALSE)
Parameters:
pattern: regular expressions pattern
string: character vector to be searched
ignore.case: whether to ignore case in the search. Here ignore.case is an optional parameter as is set to FALSE by default.
Here we have to pass the string to be searched in the grepl() function and the column to search in, this function returns true or false according to which filter() function prints the rows.
Syntax: df %>% filter(grepl(‘Pattern’, column_name))
Parameters:
df: Dataframe object
grepl(): finds the pattern String
“Pattern”: pattern(string) to be found
column_name: pattern(string) will be searched in this column
Example: 
R
library(dplyr)df <- data.frame( marks = c(20.1, 30.2, 40.3, 50.4, 60.5),                                    age = c(21:25),                   roles = c('Software Eng.', 'Software Dev',                            'Data Analyst', 'Data Eng.',                            'FrontEnd Dev'))  df %>% filter(grepl('Dev', roles))
Output:
  marks age        roles
1  30.2  22 Software Dev
2  60.5  25 FrontEnd Dev
Note the only difference in this code from the above approach is that here we are using a ‘!‘ not operator, this operator inverts the output provided by the grepl() function by converting TRUE to FALSE and vice versa, this in result only prints the rows which does not contain the patterns and filter outs the rows containing the pattern.
Syntax: df %>% filter(!grepl(‘Pattern’, column_name))
Parameters:
df: Dataframe object
grepl(): finds the pattern String
“Pattern“: pattern(string) to be found
column_name: pattern(string) will be searched in this column
Example: 
R
library(dplyr)  df <- data.frame( marks = c(20.1, 30.2, 40.3, 50.4, 60.5),                                    age = c(21:25),                   roles = c('Software Eng.', 'Software Dev',                           'Data Analyst', 'Data Eng.',                           'FrontEnd Dev'))  df %>% filter(!grepl('Eng.', roles))
Output:
  marks age        roles
1  30.2  22 Software Dev
2  40.3  23 Data Analyst
3  60.5  25 FrontEnd Dev
This code is also similar to the above approaches the only difference is that while passing the multiple patterns(string) in the grepl() function, the patterns are separated with the OR(‘ | ‘) operator. This prints all the rows containing the specified pattern.
Syntax: 
df %>% filter(grepl(‘Patt.1 | Patt.2‘, column_name))
Example:
R
library(dplyr)  df <- data.frame( marks = c(20.1, 30.2, 40.3, 50.4, 60.5),                                    age = c(21:25),                   roles = c('Software Eng.', 'Software Dev',                            'Data Analyst', 'Data Eng.',                           'FrontEnd Dev'))  df %>% filter(grepl('Dev|Eng.', roles))
Output:
 marks age         roles
1  20.1  21 Software Eng.
2  30.2  22  Software Dev
3  50.4  24     Data Eng.
4  60.5  25  FrontEnd Dev
This code is similar to the above approach, the only difference is that we are using ‘!‘ not operator, this operator inverts the output provided by the grepl() function by converting TRUE to FALSE and vice versa, this in result only prints the rows which do not contain the specified multiple patterns and filter outs the rows containing the patterns.
Syntax: 
df %>% filter(!grepl(‘Patt.1 | Patt.2’, column_name))
Example: 
R
library(dplyr)  df <- data.frame( marks = c(20.1, 30.2, 40.3, 50.4, 60.5),                                    age = c(21:25),                   roles = c('Software Eng.', 'Software Dev',                            'Data Analyst', 'Data Eng.',                           'FrontEnd Dev'))  df %>% filter(!grepl('Data|Front', roles))
Output:
  marks age         roles
1  20.1  21 Software Eng.
2  30.2  22  Software Dev
Picked
R Dplyr
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
R - if statement
Time Series Analysis in R
How to filter R dataframe by multiple conditions?