Learn R Programming

⚠️There's a newer version (1.5.0) of this package.Take me there.

matrixStats: Functions that Apply to Rows and Columns of Matrices (and to Vectors)

The matrixStats package provides highly optimized functions for computing common summaries over rows and columns of matrices, e.g. rowQuantiles(). There are also functions that operate on vectors, e.g. logSumExp(). Their implementations strive to minimize both memory usage and processing time. They are often remarkably faster compared to good old apply() solutions. The calculations are mostly implemented in C, which allow us to optimize beyond what is possible to do in plain R. The package installs out-of-the-box on all common operating systems, including Linux, macOS and Windows.

Example

With a matrix

> X <- matrix(rnorm(20 * 500), nrow = 20, ncol = 500)

it is many times faster to calculate medians column by column using

> mu <- matrixStats::colMedians(X)

than using

> mu <- apply(X, MARGIN=2, FUN=median)

Moreover, if performing calculations on a subset of rows and/or columns, using

> mu <- colMedians(X, rows=33:158, cols=1001:3000)

is much faster and more memory efficient than

> mu <- apply(X[33:158,1001:3000], MARGIN=2, FUN=median)

Benchmarks

For formal benchmarking of matrixStats functions relative to alternatives, see the Benchmark reports.

Installation

R package matrixStats is available on CRAN and can be installed in R as:

install.packages('matrixStats')

Pre-release version

To install the pre-release version that is available in branch develop, use:

source('http://callr.org/install#HenrikBengtsson/matrixStats@develop')

This will install the package from source. Because of this and because this package also compiles native code, Windows users need to have Rtools installed and macOS users need to have Xcode installed.

Software status

Resource:CRANTravis CIAppveyor
Platforms:MultipleLinux & macOSWindows
R CMD check
Test coverage

Copy Link

Version

Install

install.packages('matrixStats')

Monthly Downloads

240,817

Version

0.51.0

License

Artistic-2.0

Issues

Pull Requests

Stars

Forks

Maintainer

Henrik Bengtsson

Last Published

January 7th, 2025

Functions in matrixStats (0.51.0)

binMeans

Fast mean calculations in non-overlapping bins
diff2

Fast lagged differences
indexByRow

Translates matrix indices by rows into indices by columns
binCounts

Fast element counting in non-overlapping bins
allocMatrix

Allocates an empty vector, matrix or array
anyMissing

Checks if there are any missing values in an object or not
rowCollapse

Extracts one cell per row (column) from a matrix
rowAvgsPerColSet

Applies a row-by-row (column-by-column) averaging function to equally-sized subsets of matrix columns (rows)
meanOver

Fast averaging over subset of vector elements
matrixStats-package

Package matrixStats
rowOrderStats

Gets an order statistic for each row (column) in a matrix
rowIQRs

Estimates of the interquartile range for each row (column) in a matrix
rowProds

Calculates the product for each row (column) in a matrix
rowMedians

Calculates the median for each row (column) in a matrix
rowLogSumExps

Accurately computes the logarithm of the sum of exponentials across rows or columns
rowRanges

Gets the range of values in each row (column) of a matrix
rowQuantiles

Estimates quantiles for each row (column) in a matrix
rowDiffs

Calculates difference for each row (column) in a matrix
rowCumsums

Cumulative sums, products, minima and maxima for each row (column) in a matrix
rowCounts

Counts the number of TRUE values in each row (column) of a matrix
rowWeightedMeans

Calculates the weighted means for each row (column) in a matrix
rowRanks

Gets the rank of each row (column) of a matrix
rowTabulates

Tabulates the values in a matrix by row (column)
rowWeightedMedians

Calculates the weighted medians for each row (column) in a matrix
rowSds

Standard deviation estimates for each row (column) in a matrix
validateIndices

Validate indices
sumOver

Fast sum over subset of vector elements
rowVars

Variance estimates for each row (column) in a matrix
signTabulate

Calculates the number of negative, zero, positive and missing values
varDiff

Estimation of scale based on sequential-order differences
weightedMean

Weighted Arithmetic Mean
xUNDERSCOREOPUNDERSCOREy

Fast calculation of 'z <- x OP y' and 'z <- t(t(x) OP y)'
weightedVar

Weighted variance and weighted standard deviation
weightedMad

Weighted Median Absolute Deviation (MAD)
weightedMedian

Weighted Median Value
logSumExp

Accurately computes the logarithm of the sum of exponentials