R/wtd_median.R

Defines functions wtd_median

Documented in wtd_median

#' Weighted median
#'
#' Compute the median of \code{x} weighted by \code{weights}.
#'
#' If \code{weights} is \code{NULL}, all elements of \code{x} receive the
#' same weight. Observations with zero weights are omitted from the
#' computation. Missing values are not handled specially and
#' produce a missing value as the result.
#'
#' @param x a numerical vector.
#' @param weights a numerical vector of weights the same length as \code{x}.
#'
#' @return The weighted median of \code{x}.
#'
#' @examples
#' wtd_median(1:5, weights = 5:1)
#'
#' @family weighted statistics
#' @export
wtd_median <- function(x, weights = NULL) {
  wtd_quantile(x, weights, probs = 0.5)
}
jcpernias/ec1047 documentation built on Nov. 19, 2020, 2:33 a.m.