Appendix E. Matrix Algebra in R

Many of the functions described in this book operate on matrices. The manipulation of matrices is built deeply into the R language. Table E.1 describes operators and functions that are particularly important for solving linear algebra problems. In the following table, A and B are matrices, x and b are vectors, and k is a scalar.

Table E.1. R functions and operators for matrix algebra

Operator or Function

Description

+ - * / Λ Element-wise addition, subtraction, multiplication, division, and exponentiation, respectively.
A %*% B Matrix multiplication.
A %o% B Outer product. AB'.
cbind(A, B, ...) Combine matrices or vectors horizontally.
chol(A) Choleski factorization of A. If R <- chol(A), then chol(A) contains the upper triangular factor, such that R’R = A.
colMeans(A) Returns a vector containing the column means of A.
crossprod(A) A’A.
crossprod(A,B) A’B.
colSums(A) Returns a vector containing the column sums of A.
diag(A) Returns a vector containing the elements of the principal diagonal.
diag(x) Creates a diagonal matrix with the elements of x in the principal diagonal.
diag(k) If k is a scalar, this creates a k x k identity matrix.
eigen(A) Eigenvalues and eigenvectors of A. If y <- eigen(A), then
y$val are the eigenvalues of A and
y$vec are the eigenvectors of A.
ginv(A) Moore-Penrose Generalized Inverse of A. (Requires the MASS package).
qr(A) QR decomposition of A. If y <- qr(A), then
y$qr has an upper triangle containing the decomposition and a lowertriangle that contains information on the decomposition,
y$rank is the rank of A,
y$qraux is a vector containing additional information on Q, and
y$pivot contains information on the pivoting strategy used.
rbind(A, B, ...) Combines matrices or vectors vertically.
rowMeans(A) Returns a vector containing the row means of A.
rowSums(A) Returns a vector containing the row sums of A.
solve(A) Inverse of A where A is a square matrix.
solve(A, b) Solves for vector x in the equation b = Ax.
svd(A) Single value decomposition of A. If y <- svd(A), then
y$d is a vector containing the singular values of A,
y$u is a matrix with columns containing the left singular vectors of A, and
y$v is a matrix with columns containing the right singular vectors of A.
t(A) Transpose of A.

There are several user-contributed packages that are particularly useful for matrix algebra. The matlab package contains wrapper functions and variables used to replicate MATLAB function calls as closely as possible. These functions can help port MATLAB applications and code to R. There’s also a useful cheat sheet for converting MATLAB statements to R statements at http://mathesaurus.sourceforge.net/octave-r.html.

The Matrix package contains functions that extend R in order to support highly dense or sparse matrices. It provides efficient access to BLAS (Basic Linear Algebra Subroutines), Lapack (dense matrix), TAUCS (sparse matrix), and UMFPACK (sparse matrix) routines.

Finally, the matrixStats package provides methods for operating on the rows and columns of matrices, including functions that calculate counts, sums, products, central tendency, dispersion, and more. Each is optimized for speed and efficient memory use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset