rowmean: Compute column means based on a grouping variable

Description Usage Arguments Details Value Author(s) Examples

View source: R/rowmean.R

Description

Computes the column mean or median for each group of rows in a matrix.

Usage

1
2
3

Arguments

x

A numeric matrix or matrix-like object.

group

A vector or factor specifying the group assignment for each row of x. Alternatively, a matrix of soft assignments for each row to each group (column).

Details

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.

Value

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.

Author(s)

Aaron Lun

Examples

1
2
3
4
x <- matrix(runif(100), ncol = 5)
group <- sample(1:8, 20, TRUE)
(xmean <- rowmean(x, group))
(xmeds <- rowmedian(x, group))

LTLA/TrajectoryUtils documentation built on Aug. 8, 2021, 8:51 a.m.