#### Outlier detection ######
x<-c(10,14,12,15,23,13,19,21,16,17,25) # x is a vector of length 11
length(x)
## [1] 11
y<-c(10,14,12,15,23,13,19,21,16,17,42)
z<-c(13,14,12,15,23,21,28,31,32,69,73)
quantile(x,.25)#Q1
## 25%
## 13.5
quantile(x,.50)#Q2
## 50%
## 16
quantile(x,.75)#Q3
## 75%
## 20
IQR(x)# IQR of x
## [1] 6.5
#### Function for detecting outlier #######
Outlier<-function(x){
lf<-quantile(x,.25)-1.5*IQR(x)
uf<-quantile(x,.75)+1.5*IQR(x)
outlier_index<-x<lf|x>uf
x_outliers<-x[outlier_index]
if(length(x_outliers)==1){
cat("The outlier is :",x_outliers)
}else if(length(x_outliers)>1){
cat("The outliers are :",x_outliers)
}else{
cat("There is no outlier")
}
par(mfrow=c(1,2))
boxplot(x,main="Boxplot",col="steelblue")
hist(x,main = "Histogram",col="grey")
}
Outlier(x)
## There is no outlier

Outlier(y)
## The outlier is : 42

Outlier(z)
## The outliers are : 69 73
