Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

x_OP_y

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


Description

Fast calculation of 'z <- x OP y' and 'z <- t(t(x) OP y)', where OP can be +, -, *, and /. For + and *, na.rm = TRUE will drop missing values first.

Usage

x_OP_y(x, y, OP, xrows = NULL, xcols = NULL, yidxs = NULL,
  commute = FALSE, na.rm = FALSE)

t_tx_OP_y(x, y, OP, xrows = NULL, xcols = NULL, yidxs = NULL,
  commute = FALSE, na.rm = FALSE)

Arguments

x

A numeric or logical NxK matrix.

y

A numeric or logical vector of length L.

OP

A character specifying which operator to use.

xrows, xcols

A vector indicating subset of rows (and/or columns) to operate over 'x'. If NULL, no subsetting is done.

commute

If TRUE, 'y OP x' ('t(y OP t(x))') is calculated, otherwise 'x OP y' ('t(t(x) OP y)').

na.rm

If TRUE, missing values are ignored, otherwise not.

idxs

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

...

Not used.

Value

Returns a numeric NxK matrix.

Missing values

If na.rm = TRUE, then missing values are "dropped" before applying the operator to each pair of values. For instance, if x[1, 1] is a missing value, then the result of x[1, 1] + y[1] equals y[1]. If also y[1] is a missing value, then the result is a missing value. This only applies to additions and multiplications. For subtractions and divisions, argument na.rm is ignored.

Author(s)

Henrik Bengtsson

Examples

x <- matrix(c(1, 2, 3, NA, 5, 6), nrow = 3, ncol = 2)

# Add 'y' to each column
y <- 1:2
z0 <- x + y
z1 <- x_OP_y(x, y, OP = "+")
print(z1)
stopifnot(all.equal(z1, z0))


# Add 'y' to each row
y <- 1:3
z0 <- t(t(x) + y)
z1 <- t_tx_OP_y(x, y, OP = "+")
print(z1)
stopifnot(all.equal(z1, z0))

matrixStats

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

v0.58.0
Artistic-2.0
Authors
Henrik Bengtsson [aut, cre, cph], Constantin Ahlmann-Eltze [ctb], Hector Corrada Bravo [ctb], Robert Gentleman [ctb], Jan Gleixner [ctb], Peter Hickey [ctb], Ola Hossjer [ctb], Harris Jaffee [ctb], Dongcan Jiang [ctb], Peter Langfelder [ctb], Brian Montgomery [ctb], Hugh Parsonage [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.