Skip to contents

Detecting outliers in numerical columns based on Tukey's method to identify outliers ranged above and below the 1.5*IQR.

Usage

srv_detect_outliers(
  x,
  replace = FALSE,
  type = c("mean", "median", "NA"),
  value = NULL
)

Arguments

x

A numerical vector to check for outliers.

replace

Replace identified outliers with type?

type

What to replace the identified outliers with. Can be the "mean", "median", or "NA".

value

If a value is supplied, the outliers will be replaced with said value. Will always overwrite the "type" parameter if also supplied.

Value

An outlier comparison and a vector with the same values - either removed or substitated depending on your selection.

Examples

x <- iris$Sepal.Width
library(dplyr)
# Outlier detection & removal if desirable
y <- srv_detect_outliers(x, replace = TRUE, type = "mean")

#> Outliers identified: 4 from 150 observations
#> Proportion (%) of outliers: 2.67
#> Mean of the outliers: 3.67
#> Mean without removing outliers: 3.057
#> Mean if we remove outliers: 3.041
#> 
#> Outliers successfully replaced
y <- srv_detect_outliers(x, replace = TRUE, type = "NA")

#> Outliers identified: 4 from 150 observations
#> Proportion (%) of outliers: 2.67
#> Mean of the outliers: 3.67
#> Mean without removing outliers: 3.057
#> Mean if we remove outliers: 3.04
#> 
#> Outliers successfully replaced
a <- srv_detect_outliers(x, replace = TRUE, type = "NA")
#> Outliers identified: 4 from 150 observations
#> Proportion (%) of outliers: 2.67
#> Mean of the outliers: 3.67
#> Mean without removing outliers: 3.057
#> Mean if we remove outliers: 3.04
#> 
#> Outliers successfully replaced
sum(is.na(a))
#> [1] 4

z <- srv_detect_outliers(x, replace = FALSE, type = "NA")
#> Outliers identified: 4 from 150 observations
#> Proportion (%) of outliers: 2.67
#> Mean of the outliers: 3.67
#> Mean without removing outliers: 3.057
#> Mean if we remove outliers: 3.04
#> 
#> Nothing changed
sum(is.na(z))
#> [1] 0

# And in a pipe:

abc <-
  iris %>%
  mutate(Sepal.Width.Replaced = srv_detect_outliers(Sepal.Width,
    replace = TRUE, type = "NA"
  ))
#> Outliers identified: 4 from 150 observations
#> Proportion (%) of outliers: 2.67
#> Mean of the outliers: 3.67
#> Mean without removing outliers: 3.057
#> Mean if we remove outliers: 3.04
#> 
#> Outliers successfully replaced

bcd <-
  iris %>%
  mutate(Sepal.Width.Replaced = srv_detect_outliers(Sepal.Width,
    replace = FALSE
  ))

#> Outliers identified: 4 from 150 observations
#> Proportion (%) of outliers: 2.67
#> Mean of the outliers: 3.67
#> Mean without removing outliers: 3.057
#> Mean if we remove outliers: 3.041
#> 
#> Nothing changed

xyz <-
  iris %>%
  mutate(Sepal.Width.Replaced = srv_detect_outliers(Sepal.Width,
    replace = TRUE, value = 25
  ))

#> Outliers identified: 4 from 150 observations
#> Proportion (%) of outliers: 2.67
#> Mean of the outliers: 3.67
#> Mean without removing outliers: 3.057
#> Mean if we remove outliers: 3.626
#> 
#> Outliers successfully replaced

mean(abc$Sepal.Width.Replaced, na.rm = TRUE)
#> [1] 3.040411
mean(bcd$Sepal.Width.Replaced)
#> [1] 3.057333
mean(xyz$Sepal.Width.Replaced)
#> [1] 3.626