Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
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.
| 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. |