#' Replace outliers from a numeric vector.
#'
#' Replace outliers of a numeric vector. Another name for this data processing step is "windsorization". This function defines outliers as anything above the 95th percentile, or below the 5th. It will replace values larger or smaller than this with the percentile value.
#' @param x Numeric vector to be windsorized.
#' @keywords windsorization, outliers
#' @export
#' @examples
#' replace_outliers(c(rnorm(10,10,1), 2, 15))
replace_outliers <- function(x){ #Takes a vector, column or row and returns vector, column or row
first <- quantile(x, 0.05) # first limit
last <- quantile(x, 0.95) # last limit
x[x<first] <- first #Filter
x[x>last] <- last
return(x)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.