rowmean: Compute column means based on a grouping variable

View source: R/rowmean.R

rowmeanR Documentation

Compute column means based on a grouping variable

Description

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

Usage

rowmean(x, group)

rowmedian(x, group)

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

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 Feb. 5, 2024, 11:56 a.m.