Description Usage Arguments Value Examples
View source: R/matstat_utils.R
sweepMatrix
is a faster and less memory-hungry version of
sweep(x, MARGIN, STATS, FUN = c("-", "+", "*", "/", "<", ...))
.
1 2 3 4 5 6 7 | sweepMatrix(
x,
MARGIN,
STATS,
FUN = c("-", "+", "*", "/", "^", "==", "!=", "<", "<=", ">", ">="),
has_NA = NULL
)
|
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 |
FUN |
character value, one of "+", "-", "*", "/", "^", "==", "!=", "<", "<=", ">=", ">" |
has_NA |
logical value indicating if there is any missing value in
|
sweepMatrix
returns a matrix of the same type as
FUN(x[1, 1], y[1])
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))))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.