Filtering rows containing Multiple patterns(strings)
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
Filtering row which contains a certain string using Dplyr in R
In this article, we will learn how to filter rows that contain a certain string using dplyr package in R programming language.