View source: R/rowWeightedMedians.R
rowWeightedMedians | R Documentation |
Calculates the weighted medians for each row (column) in a matrix.
rowWeightedMedians(x, w = NULL, rows = NULL, cols = NULL,
na.rm = FALSE, ..., useNames = TRUE)
colWeightedMedians(x, w = NULL, rows = NULL, cols = NULL,
na.rm = FALSE, ..., useNames = TRUE)
x |
An NxK |
w |
A |
rows |
A |
cols |
A |
na.rm |
If |
... |
Additional arguments passed to |
useNames |
If |
The implementations of these methods are optimized for both speed and
memory. If no weights are given, the corresponding
rowMedians
()/colMedians()
is used.
Returns a numeric
vector
of
length N (K).
Henrik Bengtsson
Internally, weightedMedian
() is used.
See rowMedians
() and colMedians()
for non-weighted
medians.
x <- matrix(rnorm(20), nrow = 5, ncol = 4)
print(x)
# Non-weighted row averages
mu_0 <- rowMedians(x)
mu <- rowWeightedMedians(x)
stopifnot(all.equal(mu, mu_0))
# Weighted row averages (uniform weights)
w <- rep(2.5, times = ncol(x))
mu <- rowWeightedMedians(x, w = w)
stopifnot(all.equal(mu, mu_0))
# Weighted row averages (excluding some columns)
w <- c(1, 1, 0, 1)
mu_0 <- rowMedians(x[, (w == 1), drop = FALSE])
mu <- rowWeightedMedians(x, w = w)
stopifnot(all.equal(mu, mu_0))
# Weighted row averages (excluding some columns)
w <- c(0, 1, 0, 0)
mu_0 <- rowMedians(x[, (w == 1), drop = FALSE])
mu <- rowWeightedMedians(x, w = w)
stopifnot(all.equal(mu, mu_0))
# Weighted averages by rows and columns
w <- 1:4
mu_1 <- rowWeightedMedians(x, w = w)
mu_2 <- colWeightedMedians(t(x), w = w)
stopifnot(all.equal(mu_2, mu_1))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.