weighted.mean
Compute Weighted Mean

Description

Computes a weighted mean from a vector and a vector of weighting values.

Usage

weighted.mean(x, w, ...)
## Default S3 method:
weighted.mean(x, w, ..., na.rm=FALSE)
## S3 method for class 'Date':
weighted.mean(x, w, ...)
## S3 method for class 'difftime':
weighted.mean(x, w, ...)
## S3 method for class 'POSIXct':
weighted.mean(x, w, ...)
## S3 method for class 'POSIXlt':
weighted.mean(x, w, ...)

Arguments

x a numeric vector. Missing values (NAs) are allowed but result in a value of NA unless you use na.rm = TRUE.
w a numeric vector, with the same length as x, where each element specifies a weighting value for the corresponding value in x. Default is no weights, which is equivalent to all 1s. Missing values (NAs) are allowed but result in a value of NA unless you use na.rm = TRUE.
... arguments to be passed to or from methods.
na.rm a logical value. If TRUE, missing values are removed from x and w before computing the weighted mean.
Value
returns the weighted mean of x, the same class attribute as input x.
Examples
weighted.mean(c(1, 2, 3), c(1, 2, 3))
# [1] 2.333333

x <- Sys.Date() weighted.mean(c(x, x+3, x+5), c(0.1, 0.2, 0.1)) x <- Sys.time() weighted.mean(c(x, x+3600, x), c(1, 2, 5))

Package stats version 6.1.1-7
Package Index