R/winsorMean.R

Defines functions winsorMean

winsorMean <- function(x, trim = 0, na.rm = FALSE, ...) {
  if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
    warning("argument is not numeric or logical: returning NA")
    return(NA)
  }
  if (na.rm)
    x <- x[!is.na(x)]
  if (!is.numeric(trim) || length(trim) != 1L)
    stop("'trim' must be numeric of length one")
  n <- length(x)
  if (trim > 0 && n) {
    if (is.complex(x))
      stop("trimmed means are not defined for complex data")
    if (trim >= 0.5)
      return(median(x, na.rm = FALSE))
    lo <- floor(n * trim) + 1
    hi <- n + 1 - lo
    x <- sort(x)
    if ( lo > 0 ) x[1:lo] <- x[lo]
    if ( hi <= n ) x[ hi:n ] <- x[hi]
  }
  mean(x)
}
kecoli/PCRM documentation built on May 7, 2022, 9:33 a.m.