R: How to Use grep() with OR


You can use the grep() function in R to find elements in a vector that match a particular pattern.

Often you may want to use the grep() function with OR logic to find elements that match at least one of several different patterns.

You can use the following basic syntax to do so:

#create new data frame that contains rows that match Mavs or Kings in team column
df_new <- df[grep('Mavs|Kings', df$team), ]

This particular example will create a new data frame that contains all rows from the original data frame that contain either the string Mavs or Kings in the team column of the original data frame.

By using the | operator in the grep() function, we are able to perform “OR” logic and match strings that contain one of several potential patterns.

The following example shows how to use this syntax in practice.

Related: R: How to Use grep() to Find Exact Match

Example: How to Use grep() with OR Logic in R

Suppose we create the following data frame in R that contains information about various basketball teams:

#create data frame
df <- data.frame(team=c('Mavs', 'Hawks', 'Nets', 'Heat', 'Cavs', 'Mavs2', 'Kings'),
                 points=c(104, 115, 124, 120, 112, 140, 112),
                 status=c('Bad', 'Good', 'Excellent', 'Great', 'Bad', 'Great', 'Bad'))

#view data frame
df

   team points    status
1  Mavs    104       Bad
2 Hawks    115      Good
3  Nets    124 Excellent
4  Heat    120     Great
5  Cavs    112       Bad
6 Mavs2    140     Great
7 Kings    112       Bad

Suppose that we would like to use the grep() function to extract each row from the data frame that contains either Mavs or Kings in the team column.

We can use the following syntax to do so:

#create new data frame that contains rows that match Mavs or Kings in team column
df_new <- df[grep('Mavs|Kings', df$team), ]

#view new data frame
df_new

   team points status
1  Mavs    104    Bad
6 Mavs2    140  Great
7 Kings    112    Bad

Notice that the new data frame only contains rows that match the pattern Mavs or Kings in the team column of the original data frame.

Specifically, we can see that the rows with the following values in the team column were matched:

  • Mavs
  • Mavs2
  • Kings

Note that you can use the  | operator as many times as you would like to match even more patterns.

For example, we could use the following syntax to match the patterns Mavs, Kings or Heat in the team column:

#create new data frame that contains rows that match one of several patterns
df_new <- df[grep('Mavs|Kings|Heat', df$team), ]

#view new data frame
df_new

   team points status
1  Mavs    104    Bad
4  Heat    120  Great
6 Mavs2    140  Great
7 Kings    112    Bad

Notice that the new data frame only contains rows that match the pattern MavsKings or Heat in the team column of the original data frame.

Feel free to use the | operator as many times as you would like in the grep() function to match as many potential patterns as you would like.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Concatenate Vector of Strings in R
How to Extract Numbers from Strings in R
How to Remove Spaces from Strings in R
How to Compare Strings in R

Leave a Reply

Your email address will not be published. Required fields are marked *