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

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

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

Arguments

x

An NxK matrix or, if dim. is specified, an N * K vector.

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.

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. Comment: The reason for this argument being named with a period at the end is purely technical (we get a run-time error if we try to name it dim).

...

Not used.

useNames

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

Value

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

See also

See also diff2().

Author

Henrik Bengtsson

Examples

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

d1 <- rowDiffs(x)
print(d1)
#>       [,1] [,2]
#>  [1,]    9    9
#>  [2,]    9    9
#>  [3,]    9    9
#>  [4,]    9    9
#>  [5,]    9    9
#>  [6,]    9    9
#>  [7,]    9    9
#>  [8,]    9    9
#>  [9,]    9    9

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