matrix_functions: Convenience Functions For Matrices

Description Usage Arguments Details Value Author(s) Examples

Description

Convenience functions for manipulating and reporting on matrices.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
allDiv(x, log=FALSE)
colDiv(x, log=FALSE)
rowDiv(x, log=FALSE)

asSymmetric(x, keep.lower=TRUE)

colSample(x, size, replace=FALSE, prob=NULL)
rowSample(x, size, replace=FALSE, prob=NULL)

colSort(x, decreasing=FALSE, na.last=NA)
rowSort(x, decreasing=FALSE, na.last=NA)

isRowMax(x)
isRowMin(x)
matrix2design(x)

labsDet(x)

matrixMaxIdx(x)
matrixMinIdx(x)

matrixTrace(x)

Arguments

x

a numeric matrix or data frame (square for asSymmetric, labsDet, and matrixTrace; of any dimension for the other functions).

log

logical; if TRUE, the natural logarithm of the result will be returned.

keep.lower

logical; if TRUE, the lower triangular portion of x will be used, otherwise the upper triangular portion.

size, replace, prob

as the equivalent arguments to sample.

decreasing, na.last

as the equivalent arguments to sort.

Details

allDiv returns x divided by the sum of all its elements, while colDiv returns the columns of x divided by their sums, and rowDiv returns the rows of x divided by their sums. Because calculations are performed by subtraction of logarithms when log is TRUE, this option may be useful for precision, e.g., exp(rowDiv(x, TRUE)) may be more precise than rowDiv(x).

If x is square, asSymmetric mirrors the lower triangular portion (by default) of x to the upper portion and returns a symmetric matrix. matrixTrace returns the trace of the matrix, that is, the sum of the elements along the diagonal. No checking is done, so it is the user's responsibility to ensure that x is square; the function will “work” with non-square matrices as well!

colSample and rowSample return a matrix consisting of samples from the columns or rows, respectively, of x.

colSort and rowSort return x with its columns sorted by the sums of its columns or its rows sorted by the sums of its rows, respectively.

isRowMax returns a matrix of the same dimension as x of which the (n,k)th element is TRUE if the (n,k)th element of x is the maximum value in the kth row, otherwise FALSE. isRowMin is the same for row minima instead of maxima. For matrix2design, the result is the same as isRowMax except that numerical values (1 or 0) are used instead of logical values.

labsDet returns the logarithm of the absolute value of the determinant of x. Like the built-in function det, this is a convenience wrapper around determinant.

matrixMaxIdx and matrixMinIdx return the row and column indices of the maximum and minimum elements, respectively, of x.

Value

For allDiv, colDiv, rowDiv, asSymmetric, isRowMax, isRowMin, matrix2design, colSort, and rowSort, a matrix of the same dimension as x. For colSample and rowSample, a matrix of the specified dimension. For matrixMaxIdx and matrixMinIdx, a two-element vector. For matrixTrace, a scalar.

Author(s)

Daniel Dvorkin

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
x <- matrix(1:12, ncol=3)
x
#     [,1] [,2] [,3]
#[1,]    1    5    9
#[2,]    2    6   10
#[3,]    3    7   11
#[4,]    4    8   12
rowDiv(x)
#           [,1]      [,2]      [,3]
#[1,] 0.06666667 0.3333333 0.6000000
#[2,] 0.11111111 0.3333333 0.5555556
#[3,] 0.14285714 0.3333333 0.5238095
#[4,] 0.16666667 0.3333333 0.5000000
rowDiv(x, log=TRUE)
#          [,1]      [,2]       [,3]
#[1,] -2.708050 -1.098612 -0.5108256
#[2,] -2.197225 -1.098612 -0.5877867
#[3,] -1.945910 -1.098612 -0.6466272
#[4,] -1.791759 -1.098612 -0.6931472
matrixMaxIdx(x)
# [1] 4 3

lcmix documentation built on May 2, 2019, 6:49 p.m.