6.4 filter()
Function: Only retain specific rows of data that meet the specified requirement(s).
Only display data from the diamonds
dataset that have a cut
value of Fair:
diamonds %>% filter(cut == "Fair")
Only display data from diamonds that have a cut
value of Fair or Good and a price at or under $600 (notice how the or statement is obtained with | while the and statement is achieved through a comma):
diamonds %>%
filter(cut == "Fair" | cut == "Good", price <= 600)
An alternative method that achieves the same outcome:
(read as: in the diamonds dataset, show me cut
values that match what’s inside the vector c("Fair", "Good")
)
diamonds %>%
filter(cut %in% c("Fair", "Good"), price <= 600)
The following code would require the cut
be Fair and Good (for which none exists):
diamonds %>%
filter(cut == "Fair", cut == "Good", price <= 600)
Only display data from diamonds that do not have a cut
value of Fair:
diamonds %>% filter(cut != "Fair")