weighted.mean
Weighted Arithmetic Mean
Compute a weighted mean.
- Keywords
- univar
Usage
weighted.mean(x, w, …)# S3 method for default
weighted.mean(x, w, …, na.rm = FALSE)
Arguments
- x
an object containing the values whose weighted mean is to be computed.
- w
a numerical vector of weights the same length as
xgiving the weights to use for elements ofx.- …
arguments to be passed to or from methods.
- na.rm
a logical value indicating whether
NAvalues inxshould be stripped before the computation proceeds.
Details
This is a generic function and methods can be defined for the first
argument x: apart from the default methods there are methods
for the date-time classes "POSIXct", "POSIXlt",
"difftime" and "Date". The default method will work for
any numeric-like object for which [, multiplication, division
and sum have suitable methods, including complex vectors.
If w is missing then all elements of x are given the
same weight, otherwise the weights coerced to numeric by
as.numeric and normalized to sum to one (if possible: if
their sum is zero or infinite the value is likely to be NaN).
Missing values in w are not handled specially and so give a
missing value as the result. However, zero weights are handled
specially and the corresponding x values are omitted from the
sum.
Value
For the default method, a length-one numeric vector.
See Also
Examples
library(stats)
# NOT RUN {
## GPA from Siegel 1994
wt <- c(5, 5, 4, 1)/15
x <- c(3.7,3.3,3.5,2.8)
xm <- weighted.mean(x, wt)
# }