R/replace_outliers.R

#' 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)
}
jemilianosf/GrisLab documentation built on May 14, 2019, 2:44 p.m.