Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tidyverse filter outliers – in one pipe

I want to filter outliers in the tidyverseframe work in one pipe.
Outlier for this example is just defined as Q1 - 1.5 * IQR and Q3 + 1.5 * IQR.
Q1 being the 25 percentile and Q3 the 75% percentile. And IQR the interquartile range, IQR = Q3 - Q1.

I managed to compute the upper and lower bound for outliers, and I am familiar with the filter() function from dplyr. However I do not know how to get the values calculated inside the summarize in the same pipe operation back to the complete data.frame

iris %>% 
  group_by(Species) %>% 
  # filter(API_Psy_dm <=)
  summarise(IQR = IQR(Sepal.Length),
            O_upper =quantile(Sepal.Length, probs=c( .75), na.rm = FALSE)+1.5*IQR,  
            O_lower =quantile(Sepal.Length, probs=c( .25), na.rm = FALSE)-1.5*IQR  
  )

Is this even possible? Or would I need a second pipe? Or is there a more convenient way than to calculate the upper and lower limit myself?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use mutate instead of summarize, and then filter:

iris %>% 
  group_by(Species) %>% 
  mutate(IQR = IQR(Sepal.Length),
            O_upper = quantile(Sepal.Length, probs=c( .75), na.rm = FALSE)+1.5*IQR,  
            O_lower = quantile(Sepal.Length, probs=c( .25), na.rm = FALSE)-1.5*IQR  
  ) %>% 
  filter(O_lower <= Sepal.Length & Sepal.Length <= O_upper)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading