Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Appendix E. Matrix Algebra in R

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 FunctionDescription
+ - * / ΛElement-wise addition, subtraction, multiplication, division, and exponentiation, respectively.
A %*% BMatrix multiplication.
A %o% BOuter 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.



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial