In addition to subsetting a data frame by columns, you can also subset a data frame by rows using dplyr’s filter() function and comparison operators! Consider an orders data frame that contains data related to the orders for an e-commerce shoe company:

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

Let’s say you want to find all orders made by customers with the first name 'Joyce'.

orders %>% filter(first_name == 'Joyce')
  • the orders data frame is piped into filter()
  • the condition first_name == 'Joyce' is given as an argument
  • a new data frame containing only the rows where first_name == 'Joyce' is returned

What if you have multiple conditions you want to be met? Not a problem! To find all orders made of faux-leather AND costing more than 25:

orders %>% filter(shoe_material == 'faux-leather',price > 25)
  • the orders data frame is again piped into filter()
  • the conditions shoe_material == 'faux-leather' and price > 25 are given as arguments
  • a new data frame containing only the rows where both conditions were met is returned

You can provide any number of conditions that you please, as long as you separate each condition by a comma as its own argument. Note: each condition that you list must be met for a row to be returned!



Filter the rows of artists where the genre is 'Rock' and save the result to rock_groups. View rock_groups.


Filter the rows of artists where the genre is 'Rock' and spotify_monthly_listeners is greater than 20000000. Save the result to popular_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?