TheGoat
TheGoat

Reputation: 2867

Remove rows from a data frame that do not meet specific criteria

In the iris dataset I wish to remove all verginica rows that do not have a petal width of 1.8 but also keep all other rows for the other species in the dataset.

Rather than union this data frame above back to all other species is it possible to achieve my goals more succinctly in a single filter?

iris %>% filter(Species != 'virginica') %>% union(iris %>% filter(Species == 'virginica' & Petal.Width == 1.8))

Upvotes: 0

Views: 146

Answers (1)

deschen
deschen

Reputation: 10996

You can simply do:

library(dplyr)
iris %>%
  filter(Species != "virginica" | (Species == 'virginica' & Petal.Width == 1.8))

Upvotes: 1

Related Questions