Skip to content

Instantly share code, notes, and snippets.

@teguhn
Last active December 20, 2016 14:22
Show Gist options
  • Save teguhn/5c0f4b948b359720fe607d353f6e9ea4 to your computer and use it in GitHub Desktop.
Save teguhn/5c0f4b948b359720fe607d353f6e9ea4 to your computer and use it in GitHub Desktop.
#returning vector without extreme/outlier values
rm.extreme <- function(v) {
q3<-quantile(v, .75)
q1<-quantile(v, .25)
iqr <- 1.5 * ( q3- q1)
sapply(v, function(x) {
x[x > (q1 - iqr) & x < (q3 + iqr)]
})
}
#returning vector containing lower and upper limit of normal values
extreme.limit <- function(v) {
q3<-quantile(v, .75)[[1]]
q1<-quantile(v, .25)[[1]]
iqr <- 1.5 * ( q3- q1)
return(c((q1 - iqr),(q3 + iqr)))
}
#returning boolean if x is an extreme value of vector v
is_extreme <- function(v, x){
lim <- extreme.limit(v)
if(x<lim[1]){return('extreme_bottom')}
else if(x>lim[2]){return('extreme_top')}
else{return('normal')}
}
#testing
tes.data <- rnorm(100)
extreme.limit(tes.data)
is_extreme(tes.data, 3)
@upha
Copy link

upha commented Dec 20, 2016

rm.extreme <- function(v) {
  q3<-quantile(v, .75)[[1]]
  q1<-quantile(v, .25)[[1]]
  iqr <- IQR(v)
  v[v > (q1 - iqr) & v < (q3 + iqr)]
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment