average: Compute weighted means and medians.

View source: R/average.R

averageR Documentation

Compute weighted means and medians.

Description

Calculate the weighted average that maximizes the maximum likelihood estimator

Usage

average(x, w = rep(1, length(x)), type = "mean", na.rm = FALSE)

Arguments

x

A numeric vector with the values to be averaged.

w

A vector of weights of the same length as x.

type

A character string, either: 'mean' or 'L2' (the default), such that a weighted mean is computed using weightedMean; or 'median' or 'L1', such that a weighted median is computed using weightedMedian.

na.rm

Should NA values be ignored? Default is na.rm = FALSE.

Value

A numeric value, representing the weighted mean or median

Examples


# Create dummy values
x = runif(10,1,100)
w = runif(10,1,10)

# Compute weighted mean
average(x, w, type = 'mean')

# Compute weighted median
average(x, w, type = 'median')

andresgmejiar/lbmech documentation built on Feb. 2, 2025, 12:37 a.m.