Home > Back-end >  Remove rows from a data frame that do not meet specific criteria
Remove rows from a data frame that do not meet specific criteria

Time:10-11

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))

CodePudding user response:

You can simply do:

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