rowQuantiles: Estimates quantiles for each row (column) in a matrix

View source: R/rowQuantiles.R

rowQuantilesR Documentation

Estimates quantiles for each row (column) in a matrix

Description

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

Usage

rowQuantiles(x, rows = NULL, cols = NULL, probs = seq(from = 0, to = 1,
  by = 0.25), na.rm = FALSE, type = 7L, digits = 7L, ...,
  useNames = TRUE, drop = TRUE)

colQuantiles(x, rows = NULL, cols = NULL, probs = seq(from = 0, to = 1,
  by = 0.25), na.rm = FALSE, type = 7L, digits = 7L, ...,
  useNames = TRUE, drop = TRUE)

Arguments

x

An integer, numeric or logical NxK matrix with N >= 0.

rows

A vector indicating subset of rows to operate over. If NULL, no subsetting is done.

cols

A vector indicating subset of columns to operate over. If NULL, no subsetting is done.

probs

A numeric vector of J probabilities in [0, 1].

na.rm

If TRUE, missing values are excluded.

type

An integer specifying the type of estimator. See quantile for more details.

digits

An integer specifying the precision of the formatted percentages. Not used when 'useNames = FALSE'. In **matrixStats** (< 0.63.0), the default used to be 'max(2L, getOption("digits"))' inline with R (< 4.1.0).

...

Additional arguments passed to quantile.

useNames

If TRUE (default), names attributes of the result are set, otherwise not.

drop

If TRUE, singleton dimensions in the result are dropped, otherwise not.

Value

Returns a NxJ (KxJ) matrix, where N (K) is the number of rows (columns) for which the J quantiles are calculated. The return type is either integer or numeric depending on type.

Author(s)

Henrik Bengtsson

See Also

quantile.

Examples

set.seed(1)

x <- matrix(rnorm(50 * 40), nrow = 50, ncol = 40)
str(x)

probs <- c(0.25, 0.5, 0.75)

# Row quantiles
q <- rowQuantiles(x, probs = probs)
print(q)
q_0 <- apply(x, MARGIN = 1, FUN = quantile, probs = probs)
stopifnot(all.equal(q_0, t(q)))

# Column IQRs
q <- colQuantiles(x, probs = probs)
print(q)
q_0 <- apply(x, MARGIN = 2, FUN = quantile, probs = probs)
stopifnot(all.equal(q_0, t(q)))

HenrikBengtsson/matrixStats documentation built on Nov. 15, 2024, 3:20 p.m.