sample_data < -
data.frame
(x=
c
(-1, 2, 3, 4, 3, 2, 3, 4, 4, 5, 10),
y=
c
(-4, 3, 5, 7, 8, 5, 9, 7, 6, 5, 10),
z=
c
(-1, 3, 2, 9, 8, 7, 0, 8, 7, 2, 13),
w=
c
(10, 0, 1, 0, 1, 0, 1, 0, 2, 2, 10))
print
(
"Display original dataframe"
)
print
(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]]), ]
}
print
(
"Remove outliers"
)
print
(dataframe)
}
remove_outlier
(sample_data,
c
(
'x'
,
'y'
,
'z'
,
'w'
))