Filter to Keep Rows matching 1 of 4 conditions

Hi all,

Beginner here. I have a large data set of players with their associated teams. I want to filter the data and only keep players from 4 teams. What is the easiest way to do this?

Hi, either the dplyr way or the base R way.

Subset rows using column values — filter • dplyr (tidyverse.org)

Subsetting and Filtering a Data Frame in R (base R) – Mitch Craver

I'm trying the dplyr method and it keeps returning "Error in UseMethod("filter") :
no applicable method for 'filter' applied to an object of class "character"". I can't figure out what that means.

Can you provide a sample of your dataset and your code? It will make it easier to work out how to do what you want to do.

1 Like
library(tidyverse)

df <- tibble(team = c("Mercedes", "Alfa Romeo", "AlphaTauri", "Alpine", "Aston Martin", "Ferrari", "Haas", "McLaren", "Williams", "Red Bull"),
       result = sample(1:30, 10))

using filter()

# simplest way
df %>% 
  filter(team %in% c("Ferrari", "Alfa Romeo", "AlphaTauri"))

# or 
df %>% 
  filter(team == "Ferrari" | team == "Alfa Romeo" | team == "AlphaTauri")

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.