weighted_mean {rvec} | R Documentation |
Calculate Weighted Summaries
Description
Calculate weighted
means
medians
MADs (mean absolute deviations)
variances
standard deviations.
These functions all work with ordinary vectors and with rvecs.
Usage
weighted_mean(x, wt = NULL, na_rm = FALSE)
## Default S3 method:
weighted_mean(x, wt = NULL, na_rm = FALSE)
## S3 method for class 'rvec'
weighted_mean(x, wt = NULL, na_rm = FALSE)
weighted_mad(x, wt = NULL, na_rm = FALSE)
## Default S3 method:
weighted_mad(x, wt = NULL, na_rm = FALSE)
## S3 method for class 'rvec'
weighted_mad(x, wt = NULL, na_rm = FALSE)
weighted_median(x, wt = NULL, na_rm = FALSE)
## Default S3 method:
weighted_median(x, wt = NULL, na_rm = FALSE)
## S3 method for class 'rvec'
weighted_median(x, wt = NULL, na_rm = FALSE)
weighted_sd(x, wt = NULL, na_rm = FALSE)
## Default S3 method:
weighted_sd(x, wt = NULL, na_rm = FALSE)
## S3 method for class 'rvec'
weighted_sd(x, wt = NULL, na_rm = FALSE)
weighted_var(x, wt = NULL, na_rm = FALSE)
## Default S3 method:
weighted_var(x, wt = NULL, na_rm = FALSE)
## S3 method for class 'rvec'
weighted_var(x, wt = NULL, na_rm = FALSE)
Arguments
x |
Quantity being summarised. An ordinary vector or an rvec. |
wt |
Weights. An ordinary vector,
an rvec, or |
na_rm |
Whether to remove |
Details
x
and wt
must have the same length.
Internally the calculations are done by
matrixStats
functions such as matrixStats::weightedMean()
and matrixStats::colWeightedMeans()
.
Value
If x
or wt
or is rvec,
then an rvec of length 1. Otherwise, a scalar.
See Also
Functions
mean()
,median()
,mad()
,var()
,sd()
for unweighted data all have methods for rvecsThe original matrixStats weighted summary functions have additional options not implemented in the functions here.
-
weighted.mean()
is a base R function for weighted data For numeric summaries of draws in an rvec, use
draws_median()
, draws_mean,draws_quantile()
,draws_fun()
.
Examples
## 'x' is rvec, 'wt' is ordinary vector
v <- rvec(list(c(1, 11),
c(2, 12),
c(7, 17)))
weights <- c(40, 80, 72)
weighted_mean(v, wt = weights)
## 'x' is ordinary vector, 'wt' is rvec
y <- c(1, 2, 3)
w <- rvec(list(c(100, 200),
c(210, 889),
c(200, 200)))
weighted_mean(y, wt = w)
weighted_mean(y, wt = w, na_rm = TRUE)