I have a large dataset of around 35000 observations and 24 variables (one of which is a time-series), but I can summarise what I want to achieve using iris.
library(tidyverse)
iris.new <- iris %>%
arrange(Species, Sepal.Length, Sepal.Width) %>%
group_by(Species)
unwanted <- iris.new %>%
filter(Sepal.Length > 5 & Sepal.Width==min(Sepal.Width))
while(nrow(unwanted)!=0) {
iris.new <- iris.new %>%
arrange(Species, Sepal.Length, Sepal.Width) %>%
group_by(Species) %>%
filter(!(Sepal.Length > 5 & Sepal.Width == min(Sepal.Width)))
unwanted <- iris.new %>%
filter(Sepal.Length > 5 & Sepal.Width==min(Sepal.Width))
}
I want to filter only Sepal.Length > 5, which has minimum Sepal.Width within observations for each Species (setosa and versicolor has none). When I got rid of the first one, I repeat the filter to see if there are any and finally used a 'while' loop to do that for me.
Is there a way to filter them without using a loop?
filter(sepal.length<=5)
??? – Onyambu