rm_outliers <- function(x, na.rm = TRUE) {
# Removes outliers from a numeric vector.
#
# Arguments:
# x {numeric} -- vector of numbers
#
# Keyword Arugments:
# na.rm {logical} -- if TRUE, remove NAs prior to removing outliers (default: TRUE)
#
# Returns:
# {numeric} -- vector with outliers removed
qnt = quantile(x, probs=c(.25, .75), na.rm = na.rm)
H = 1.5 * IQR(x, na.rm = na.rm)
y = x
y[x < (qnt[1] - H)] = NA
y[x > (qnt[2] + H)] = NA
if(na.rm==TRUE) y = as.numeric(na.omit(y))
return(y)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.