R/sd.R

Defines functions sd.trim

Documented in sd.trim

#' trimmed standard deviation
#' 
#' compute standard deviation after trimming...
#' 
#' @param x A vector
#' @param na.rm A boolean
#' @param trim Numeric where to trim (proportion)


sd.trim <- function(x, na.rm=FALSE, trim=0, ...)
{
  if(!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
    warning("argument is not numeric or logical: returning NA")
    return(NA_real_)
  }
  if(na.rm) x <- x[!is.na(x)]
  if(!is.numeric(trim) || length(trim) != 1)
    stop("'trim' must be numeric of length one")
  n <- length(x)
  if(trim > 0 && n > 0) {
    if(is.complex(x)) stop("trimmed sd are not defined for complex data")
    if(trim >= 0.5) return(0)
    lo <- floor(n * trim) + 1
    hi <- n + 1 - lo
    x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi]
  }
  x <- sd(x,na.rm,...)
}
dnacombo/MaxPac documentation built on Nov. 17, 2021, 3:36 a.m.