R/reduce3mad.R

Defines functions reduce3mad

Documented in reduce3mad

##----------------------------------------------------------------------------##
## reduce3mad: Reduce given data to med +/- 3mad
##----------------------------------------------------------------------------##

#' @name reduce3mad
#' @title Reduce given data to med +/- 3mad
#' 
#' @param x Numeric
#' @param returnBound Logical of length 1, return bounds rather than reduced set
#' 
#' @importFrom stats median mad
#' @export


reduce3mad <- function(x, returnBound = FALSE) {
  xmed <- median(x)
  xmad <- 3*mad(x)
  rmX <- x > xmed + xmad | x < xmed - xmad
  if (any(rmX)) x <- reduce3mad(x[!rmX])
  if (returnBound) return(median(x) + c(-3, 3)*mad(x))
  x
}
daynefiler/dlfUtils documentation built on Oct. 22, 2021, 9:49 a.m.