sweepMatrix: Fast replacement for 'sweep'

Description Usage Arguments Value Examples

View source: R/matstat_utils.R

Description

sweepMatrix is a faster and less memory-hungry version of sweep(x, MARGIN, STATS, FUN = c("-", "+", "*", "/", "<", ...)).

Usage

1
2
3
4
5
6
7
sweepMatrix(
  x,
  MARGIN,
  STATS,
  FUN = c("-", "+", "*", "/", "^", "==", "!=", "<", "<=", ">", ">="),
  has_NA = NULL
)

Arguments

x

numeric matrix

MARGIN

an integer index of the dimension to sweep over (1 for rows, 2 for columns). Can be a character value if the input matrix has named dimension names.

STATS

a numeric vector; the summary statistic which is to be swept out. It is recycled to match the size of MARGIN.

FUN

character value, one of "+", "-", "*", "/", "^", "==", "!=", "<", "<=", ">=", ">"

has_NA

logical value indicating if there is any missing value in x. If NULL (default), it is explicitly checked by calling anyNA on x.

Value

sweepMatrix returns a matrix of the same type as FUN(x[1, 1], y[1])

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# create a matrix and choose its first row as a statistic
mat <- matrix(1:20, 4, 5)
stat <- mat[1,]

# suppose we have missing values in the matrix and the statistic
mat[2, 3] <- NA
stat[1] <- NA

# compute and print the result
( result <- sweepMatrix(mat, 2, stat, "-") )

# check
stopifnot(all.equal(result, 
                    sweep(mat, 2, stat, "-")))
stopifnot(identical(result[1, ], c(NA, rep(0L, ncol(mat) - 1))))

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.