R/weighted_mean.R

Defines functions weighted_total weighted_mean

Documented in weighted_mean weighted_total

# weighted mean (Hajek estimator)
weighted_mean <- function(x, w, na.rm = FALSE)
{
    dat <- .check_data_weights(x, w, na.rm)
    if (is.null(dat))
        return(NA)
    else
        return(sum(dat$x * dat$w) / sum(dat$w))
}
# weighted total
weighted_total <- function(x, w, na.rm = FALSE)
{
    dat <- .check_data_weights(x, w, na.rm)
    if (is.null(dat))
        return(NA)
    else
        return(sum(dat$w * dat$x))
}

Try the robsurvey package in your browser

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

robsurvey documentation built on Jan. 6, 2023, 5:09 p.m.