sample_data <- data.frame(x=c(1, 2, 3, 4, 3, 12, 3, 4, 4, 15, 0), y=c(4, 3, 25, 7, 8, 5, 9, 77, 6, 5, 0), z=c(1, 3, 2, 90, 8, 7, 0, 48, 7, 2, 3)) print("Display original dataframe") print(sample_data) boxplot(sample_data) detect_outlier <- function(x) { Quantile1 <- quantile(x, probs=.25) Quantile3 <- quantile(x, probs=.75) IQR = Quantile3-Quantile1 x > Quantile3 + (IQR*1.5) | x < Quantile1 - (IQR*1.5) } remove_outlier <- function(dataframe, columns=names(dataframe)) { for (col in columns) { dataframe <- dataframe[!detect_outlier(dataframe[[col]]), ] } return(dataframe) } remove_outlier(sample_data, c('x', 'y', 'z'))