R/w_median.R

#' @rdname weighted
#' @param ... Further arguments passed to or from other methods.
#' @export
`w_median` <- function (x, wt = NULL, na.rm = TRUE, ...)
{
    metacall <- as.list (match.call ())

    if (inherits (x, "haven_labelled")) {
        x <- as.declared (x)
    }

    if (inherits (x, "declared")) {
        na_index <- attr (x, "na_index")
        if (length (na_index)) {
            x <- x[-na_index]
            wt <- wt[-na_index] # if wt is NULL, the result is still NULL
        }
        attributes (x) <- NULL # if passed to w_quantile, to bypass this process
    }

    if (is.null (wt)) {
        return (median (x, na.rm = na.rm, ... = ...))
    }

    x <- w_quantile (
      x, wt = wt, probs = 0.5, na.rm = na.rm, ... = ...
    )

    attributes (x) <- NULL

    return (x)
}

Try the declared package in your browser

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

declared documentation built on May 29, 2024, 12:09 p.m.