The filter() function also allows for more complex filtering with the help of logical operators! Take a look at the same orders data frame from the last exercise:

id first_name last_name email shoe_type shoe_material shoe_color price
54791 Rebecca Lindsay [email protected] clogs faux-leather black 22
53450 Emily Joyce [email protected] ballet flats faux-leather navy 32
91987 Joyce Waller [email protected] sandals fabric black 12
14437 Justin Erickson [email protected] clogs faux-leather red 22

You are interested in seeing all orders that were for 'clogs' OR that cost less than 20. Using the or operator (|):

orders %>% filter(shoe_type == 'clogs' | price < 20)
  • the orders data frame is piped into filter()
  • the compound conditional statement shoe_type == 'clogs' OR price < 20 is given as an argument
  • a new data frame is returned containing only rows where shoe_type is 'clogs' or price is less than 20

What if you want to find all orders where shoes in any color but red were purchased. Using the not or bang operator (!):

orders %>% filter(!(shoe_color == red))
  • orders is again piped into filter()
  • the condition that should not be met is wrapped in parentheses, preceded by !, and given as an argument to filter()
  • a new data frame is returned containing only rows where shoe_color is not 'red'



Filter the rows of artists where the country is 'South Korea' or the year_founded is before 2000. Save the result to korea_or_before_2000, and view it.


Filter the rows of artists where the genre is not 'Rock'. Save the result to not_rock_groups, and view it.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?