Learn
Introduction to Data Frames in R
Filtering Rows with Logic II
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 | shoe_type | shoe_material | shoe_color | price | |
---|---|---|---|---|---|---|---|
54791 | Rebecca | Lindsay | RebeccaLindsay57@hotmail.com | clogs | faux-leather | black | 22 |
53450 | Emily | Joyce | EmilyJoyce25@gmail.com | ballet flats | faux-leather | navy | 32 |
91987 | Joyce | Waller | Joyce.Waller@gmail.com | sandals | fabric | black | 12 |
14437 | Justin | Erickson | Justin.Erickson@outlook.com | 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 intofilter()
- the compound conditional statement
shoe_type == 'clogs'
ORprice < 20
is given as an argument - a new data frame is returned containing only rows where
shoe_type
is'clogs'
orprice
is less than20
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 intofilter()
- the condition that should not be met is wrapped in parentheses, preceded by
!
, and given as an argument tofilter()
- a new data frame is returned containing only rows where
shoe_color
is not'red'
Instructions
1.
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.
2.
Filter the rows of artists
where the genre is not 'Rock'
. Save the result to not_rock_groups
, and view it.