How to Filter Rows that Contain a Certain String Using dplyr

pranav sirsufale


Often you may want to contain a certain string. Fortunately this is easy filter rows in a data frame in R that to do using the filter() function from the dplyr package and the grepl() function in Base R.

phd_all <- bamu %>% filter(grepl('DOCTOR OF PHILOSOPHY', bamu$`PROGRAMME NAME`))

View(phd_all)

This tutorial shows several examples of how to create all phd student’s data frame ( there is column name called $`PROGRAMME NAME` in which we have to filter those students who’s program is PHD that means that should be consist the 'DOCTOR OF PHILOSOPHY'

df <- data.frame(player = c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center'),
                 points = c(12, 15, 19, 22, 32),
                 rebounds = c(5, 7, 7, 12, 11))

Example 1: Filter Rows that Contain a Certain String

The following code shows how to filter rows that contain a certain string:

#load dplyr package

library(dplyr)

#filter rows that contain the string 'Guard' in the player column

df %>% filter(grepl('Guard', player))

player points rebounds

1 P Guard 12 5

2 S Guard 15 7

Related: Comparing grep() vs. grepl() in R: What’s the Difference?

Example 2: Filter Rows that Contain at Least One String

The following code shows how to filter rows that contain ‘Guard’ or ‘Forward’ in the player column:

#filter rows that contain 'Guard' or 'Forward' in the player column

df %>% filter(grepl('Guard|Forward', player))

player points rebounds

1 P Guard 12 5

2 S Guard 15 7

3 S Forward 19 7

4 P Forward 22 12

The following code shows how to filter rows that contain ‘P’ or ‘Center’ in the player column:

#filter rows that contain 'P' or 'Center' in the player column

df %>% filter(grepl('P|Center', player))

player points rebounds

1 P Guard 12 5

2 P Forward 22 12

3 Center 32 11

Example 3: Filter Out Rows that Contain a Certain String

The following code shows how to filter out (i.e. remove) rows that contain ‘Guard’ in the player column:

#filter out rows that contain 'Guard' in the player column

df %>% filter(!grepl('Guard', player))

player points rebounds

1 S Forward 19 7

2 P Forward 22 12

3 Center 32 11

The following code shows how to filter out (i.e. remove) rows that contain ‘Guard’ or ‘Center’ in the player column:

#filter out rows that contain 'Guard' or 'Center' in the player column

df %>% filter(!grepl('Guard|Center', player))

player points rebounds

1 S Forward 19 7

2 P Forward 22 12

0
Subscribe to my newsletter

Read articles from pranav madhukar sirsufale directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

pranav madhukar sirsufale
pranav madhukar sirsufale

🚀 Tech Enthusiast | Computer Science Graduate | Passionate about web development, app development, and data science. Skilled in Java, Node.js, React, HTML, MySQL, and Python. Always learning and sharing insights on tech, programming tutorials, and practical guides.