Search code examples
rdataframefilterpurrrtibble

Filter with map function in R


I am trying to filter multiple columns (15) of a dataframe. specifically I want to remove the outliers using Q3 + IQR1.5 and Q1 - IQR1.5 method.

Toy example:

library(tidyverse)
aa <- c(2,3,4,3,2,2,1,6,5,4,3,1,15)
bb <- c(0.2,20,30,40,30,20,20,10,30,40,30,10,10)
cc <- c(-9,2,3,4,3,2,2,1,5,4,3,1,25)

df <- tibble(aa,bb,cc)

I tried without success:

i <- NULL
for(i in 1:ncol(fat)){
   po <- fat %>% 
     filter(.[[i]] >= (quantile(.[[i]], .25) - IQR(.[[i]]) * 1.5))
   
   po <- fat %>% 
     filter(.[[i]] <= (quantile(.[[i]], .75) + IQR(.[[i]]) * 1.5))
}

Can I use filter and map functions to do this? and how?

Many thanks GS


Solution

  • We may use filter with if_all/across

    library(dplyr)
    df %>%
        filter(if_all(where(is.numeric), ~ (.>= (quantile(., .25) - IQR(.) * 1.5 )) &
               (.<= (quantile(., .75) + IQR(.) * 1.5 ))))