Skip to contents

Compute outliers for all columns of a numeric matrix using the IQR method. For each column of the input matrix, a value is called as an outlier if the value is less than the first quartile i.e. Q1 - IQR * scale factor or greater than Q3 + IQR * scale factor.

Usage

outliers_by_iqr(X, scale_factor = 1.5)

Arguments

X

numeric matrix or data.frame that can be converted to a numeric matrix with variables in the columns and sample names in the rows.

scale_factor

numeric. Factor to scale the outlier range. default 1.5.

Value

numeric matrix where values of 1L indicates an outlier and 0L indicates non-outlier

Examples

set.seed(12345)
M <- matrix(
  data = c(rnorm(10, 10, 1), rnorm(10, 100, 15)),
  ncol = 2,
  dimnames = list(paste0("sample", 1:10), c("var1", "var2"))
)

# Create one outlier in first and last rows
M[1, 1] <- 100
M[1, 2] <- 1000
M[10, 1] <- -10
M[10, 2] <- 0

# Show outliers on boxplot
boxplot(M)


# Call outliers in each column
outliers_by_iqr(M)
#>          var1 var2
#> sample1     1    1
#> sample2     0    0
#> sample3     0    0
#> sample4     0    0
#> sample5     0    0
#> sample6     0    0
#> sample7     0    0
#> sample8     0    0
#> sample9     0    0
#> sample10    1    1