The aim is to remove outliers in a vector.
x = datasets::islands ($area)
x = 12 13 13 13 14 14 15 15 16 16 16 19 21 23 25 26 29 29 30 30
32 33 36 40 42 43 43 44 49 58 73 82 82 84 89 183 184 227 280 306
840 2968 3745 5500 6795 9390 11506 16988
so far by using
x_rm_out <- x[!x%in% boxplot.stats
(x, coef = .05, do.conf = TRUE, do.out = TRUE)$out]
Result
[1] 12 13 13 13 14 14 15 15 16 16 16 19 21 23 25 26 29 29 30 30 32 33 36 40 42 43 43 44 49 58 73
[32] 82 82 84 89 183 184
Is there a way to remove 183 & 184 from vector (x)?
>Solution :
A very easy way to find outliers is with the rstatix package, then filter them out with dplyr:
# Load library:
library(rstatix)
library(dplyr)
# Make x into dataframe:
x <- data.frame(x)
# Identify outliers:
x %>%
identify_outliers()
You should get an output like this now:
x is.outlier is.extreme
1 840 TRUE TRUE
2 2968 TRUE TRUE
3 3745 TRUE TRUE
4 5500 TRUE TRUE
5 6795 TRUE TRUE
6 9390 TRUE TRUE
7 11506 TRUE TRUE
8 16988 TRUE TRUE
Now you have to filter out the data, which you can then turn into a new dataframe (< 840). You may also remove them with your previously established criterion (< 183) if you desire:
# Filter outliers and create new file:
x2 <- x %>%
filter(x < 183)
x2
Which after you enter x2, gives you this output without outliers:
x
1 12
2 13
3 13
4 13
5 14
6 14
7 15
8 15
9 16
10 16
11 16
12 19
13 21
14 23
15 25
16 26
17 29
18 29
19 30
20 30
21 32
22 33
23 36
24 40
25 42
26 43
27 43
28 44
29 49
30 58
31 73
32 82
33 82
34 84
35 89