I am supposed to use the 1.5*IQR rule to determine outliers on the left and right tail by using these two equations in a function:
This is what I have tried so far:
IQR.outliers <- function(x) { if(any(is.na(x))) stop("x is missing values") if(!is.numeric(x)) stop("x is not numeric") Q3<-quantile(x,0.75) Q1<-quantile(x,0.25) IQR<-(Q3-Q1) left<-x<(Q1-(1.5*IQR)) right<-x>(Q3+(1.5*IQR)) c(left,right) }
At the moment you are getting a vector of logicals. Instead use logical indexing. Assuming you just want the values themselves and not their positions, then:
IQR.outliers <- function(x) {
if(any(is.na(x)))
stop("x is missing values")
if(!is.numeric(x))
stop("x is not numeric")
Q3<-quantile(x,0.75)
Q1<-quantile(x,0.25)
IQR<-(Q3-Q1)
left<- (Q1-(1.5*IQR))
right<- (Q3+(1.5*IQR))
c(x[x <left],x[x>right])
}
Check to see if it gives sensible results:
> IQR.outliers (1:100)
integer(0)
> IQR.outliers (c(1:100,1000))
[1] 1000
> IQR.outliers (rnorm(1000) )
[1] -3.522064 -3.296682 2.910200 2.671333
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.