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

rowDiffs

Calculates difference for each row (column) in a matrix


Description

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

Usage

rowDiffs(x, rows = NULL, cols = NULL, lag = 1L, differences = 1L,
  dim. = dim(x), ...)

colDiffs(x, rows = NULL, cols = NULL, lag = 1L, differences = 1L,
  dim. = dim(x), ...)

Arguments

x

A numeric NxK matrix.

rows, cols

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

lag

An integer specifying the lag.

differences

An integer specifying the order of difference.

dim.

An integer vector of length two specifying the dimension of x, also when not a matrix.

...

Not used.

Value

Returns a numeric Nx(K-1) or (N-1)xK matrix.

Author(s)

Henrik Bengtsson

See Also

See also diff2().

Examples

x <- matrix(1:27, ncol = 3)

d1 <- rowDiffs(x)
print(d1)

d2 <- t(colDiffs(t(x)))
stopifnot(all.equal(d2, d1))

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.