R Matrices

R matrices are two-dimensional vectors. A matrix starts with a vector and then adds dimension or dim information (e.g. rows and columns), which is stored in a memory slot for the matrix class.  The optional slot, .dimnames, holds row and column names (and their analogues for higher dimension arrays).

Initializing R Matrices

Initializing a matrix is similar to vector creation:

A unit matrix is easy to specify with the diag() function:

Any vector can be turned into a matrix simply by specifying its .dim slot.  To use dim(), you assign a vector of two integers specifying the number of rows and columns. For example:

To combine vectors and matrices, use the functions cbind() and rbind(). The cbind() function combines vectors column by column, and rbind() combines vectors row by row:

When vectors of different lengths are combined using cbind() or rbind(), the shorter ones are replicated cyclically so that the matrix is “filled in.” If matrices are combined, they must have matching numbers of rows when using cbind() and matching numbers of columns when using rbind() to avoid errors.

The function matrix() is used to convert objects to matrices. For example, c() will combine objects into a vector and matrix() will “fold” the group by specifying the number of columns or rows:

If you provide fewer values than arguments to matrix(), the values are replicated cyclically until the matrix is filled in. If you provide more data than necessary, excess values are discarded. 

If either ncol or nrow is provided but not both, the missing argument is computed using the following relations:

  • nrow – The smallest integer equal to or greater than the number of values divided by the number of columns
  • ncol – The smallest integer equal to or greater than the number of values divided by the number of rows

Thus, nrow and ncol are computed to create the smallest matrix from all the values.

By default, values are placed in a matrix by column (e.g. the byrow argument is FALSE).  This construction is known as N-folding:

Alternatively, when the the byrow argument is set to TRUE, matrix construction is achieved via Z-Folding.  This is especially useful when reading in data from a text file arranged in a table or via the scan() function:

Naming Matrix Rows and Columns

For a vector, names are assigned to each value with using the names() function. For matrices, names are assigned to the rows and columns using dimnames() and the list() function. The list() function need only be used once and subscripting can be used thereafter.

To handle large matrices, create a list with two components, one for rows and one for columns:

To suppress row or column labels, use the NULL value for the corresponding component of the list. For example, to suppress the row labels and number the columns:

Finally, to specify the row and column labels when defining a matrix with matrix(), use the argument dimnames as follows:

Matrix Operators in R

The diag() command can be used either to create a diagonal matrix or to extract the diagonal matrix:

Meanwhile, the convenience functions row() and col() can be used to extract the upper or lower triangular values:

Assume next we have the matrix size as defined as follows:

The function apply(x, MARGIN, FUN) can be used to call a function, which operates across the columns (MARGIN = 2) or the rows (MARGIN = 1) of the matrix.  For example:

The sweep(x, MARGIN, STATS, FUN=”-“) function is similar, but defines differences between a matrix and the STATS input:

sweep() can also be used to manipulate all elements row by row or column by column, as shown below:

The following table defines common matrix operators:

[table id=13 /]

Back | Next