Skip to contents

This function identifies outliers in a numeric variable of a data.frame using the interquartile range (IQR) method.

Usage

find_outliers(data, variable_name)

Arguments

data

A data.frame object.

variable_name

A symbol representing a numeric variable in data.

Value

A data.frame containing the outliers in variable_name. If no outliers are found, the function returns NULL. The function also prints diagnostic information about the variable and the number of outliers found.

Examples

data(mtcars)
find_outliers(mtcars, mpg)
#> Variable name: mpg
#> Q1: 15.425 Q3: 22.8 IQR: 7.375
#> Upper fence: 33.8625 Lower fence: 4.3625
#> Number of outliers: 1
#>                 mpg cyl disp hp drat    wt qsec vs am gear carb
#> Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1
find_outliers(mtcars, wt)
#> Variable name: wt
#> Q1: 2.58125 Q3: 3.61 IQR: 1.02875
#> Upper fence: 5.153125 Lower fence: 1.038125
#> Number of outliers: 3
#>                      mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
#> Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
#> Chrysler Imperial   14.7   8  440 230 3.23 5.345 17.42  0  0    3    4