[R] Weighted median
David Brahm
brahm at alum.mit.edu
Thu Feb 7 16:13:43 CET 2002
I noticed too late that my weighted quantile function contains some home-grown
functions which will not be found on your system. Here's a version that is
self-contained:
g.quantile <- function(x, probs=seq(0,1,.25), wt, na.rm=TRUE) {
if (missing(wt)) return(quantile(x, probs, na.rm))
q <- !is.na(x) & !is.na(wt)
if (!all(q)) {if (na.rm) {x<-x[q]; wt<-wt[q]} else stop("NA's")}
ord <- order(x)
z <- list(y=x[ord], wt=wt[ord])
z$x <- (cumsum(z$wt) - z$wt[1]) / (sum(z$wt) - z$wt[1]) # 0 to 1 inclusive
a <- approx(z$x, z$y, probs)$y
dec <- if (length(probs)>1) 2-log10(diff(range(probs))) else 2
names(a) <- paste(format(round(100*probs, dec)), "%", sep="")
a # Returns weighted quantiles
}
--
-- David Brahm (brahm at alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list