rowmean | R Documentation |
Computes the column mean or median for each group of rows in a matrix.
rowmean(x, group)
rowmedian(x, group)
x |
A numeric matrix or matrix-like object. |
group |
A vector or factor specifying the group assignment for each row of |
The naming scheme here is somewhat inspired by the rowsum
function.
Admittedly, it is rather confusing when rowMeans
computes the mean for a row across all columns
while rowmean
computes the mean for a column across a subset of rows, but there you have it.
If group
is a matrix, it is expected to contain soft assignment weights for each row in x
.
Each row of group
should contain non-negative values that sum to unity.
These are used to compute weighted means or medians via MatrixGenerics functions.
A numeric matrix with one row per level of group
,
where the value for each column contains the mean or median across the subset of rows corresponding that level.
Aaron Lun
x <- matrix(runif(100), ncol = 5)
group <- sample(1:8, 20, TRUE)
(xmean <- rowmean(x, group))
(xmeds <- rowmedian(x, group))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.