R/w.median.R

w.median <- function(x, w) {
  if (missing(w)) w <- rep(1, length(x))
  ok <- complete.cases(x, w)
  x <- x[ok]
  w <- w[ok]
  ind <- sort.list(x)
  x <- x[ind]
  w <- w[ind]
  ind1 <- min(which(cumsum(w) / sum(w) >= 0.5))
  ind2 <- if ( (w[1] / sum(w)) > 0.5) {
            1
          } else {
            max(which(cumsum(w) / sum(w) <= 0.5))
          }
  max(x[ind1], x[ind2])
}

Try the cwhmisc package in your browser

Any scripts or data that you put into this service are public.

cwhmisc documentation built on May 1, 2019, 7:55 p.m.