5.3 Managing Matrices

A matrix is a collection of data elements arranged in a two-dimensional rectangular layout. In R, the elements that make up a matrix must be of a consistant mode (i.e. all elements must be numeric, or character, etc.). Therefore, a matrix can be thought of as an atomic vector with a dimension attribute. Furthermore, all rows of a matrix must be of same length. In this section we cover the basics of managing matrices to include:

5.3.1 Creating Matrices

Matrix class objects are created using the matrix() function and specifying values for the following arguments:

  • data - Values to include in the matrix

  • ncol - Number of columns

  • nrow - Number of rows

  • byrow - Fill the matrix rows or by columns?

  • dimnames - (optional) Names applied to rows and columns

By default, byrow = FALSE and the matrix is constructed column-wise where data fills the positions starting in the “upper left” corner and running down the columns.

1 3 5
2 4 6

We can change this behavior, byrow = TRUE and the matrix is constructed row-wise where data fills the positions starting in the “upper left” corner and running across the rows.

1 2 3
4 5 6

The underlying structure of this matrix is simply an integer vector with an added 2x3 dimension attribute.

Matrices can also contain character values. Whether a matrix contains data that are of numeric or character type, all the elements must be of the same atomic mode.

a c e
b d f

Matrices can also be created by binding vectors together as columns or rows using the colum-bind cbind() and row-bind rbind() functions. However, keep in mind that the vectors that are being binded must be of equal length and mode.

v1 v2
1 5
2 6
3 7
4 8
v1 1 2 3 4
v2 5 6 7 8
v1 v2 v3
1 5 9
2 6 10
3 7 11
4 8 12

Finally, matrices may be contructed interactively using the edit( ) function. This method can be fast for new users, but requires user input and is only appropriate for contructing small matrices. To contruct a matrix using edit(), start by creating a \(1 \times 1\) numeric matrix - the value you use doesn’t matter.

Then, call edit(mat2) to bring up a spreadsheet-style editor window. Make whatever changes you like to the matrix, then select file \(\rightarrow\) close to close the window. If you call mat2 again you’ll notice that its value has not changed. This is because we created a new object but did not assign it a name. You must re-define the matrix object created by edit or your changes will not be saved.

5.3.2 Adding on to Matrices

We can leverage the cbind() and rbind() functions for adding onto matrices as well. Again, its important to keep in mind that the vectors that are being binded must be of equal length and mode to the pre-existing matrix.

v1 v2
1 5
2 6
3 7
4 8
v1 v2 v3
1 5 9
2 6 10
3 7 11
4 8 12
v1 v2
1.0 5.0
2.0 6.0
3.0 7.0
4.0 8.0
4.1 8.1

5.3.3 Adding Attributes to Matrices

As previously mentioned, matrices by default will have a dimension attribute as illustrated in the following matrix m2.

1 5 9
2 6 10
3 7 11
4 8 12

However, matrices can also have additional attributes such as row names, column names, and comments. Adding names can be done individually, meaning we can add row names or column names separately.

row1 1 5 9
row2 2 6 10
row3 3 7 11
row4 4 8 12
col1 col2 col3
row1 1 5 9
row2 2 6 10
row3 3 7 11
row4 4 8 12

Another option is to use the dimnames() function. To add row names you assign the names to dimnames(m2)[[1]] and to add column names you assign the names to dimnames(m2)[[2]].

col1 col2 col3
row_1 1 5 9
row_2 2 6 10
row_3 3 7 11
row_4 4 8 12
col_1 col_2 col_3
row_1 1 5 9
row_2 2 6 10
row_3 3 7 11
row_4 4 8 12

Lastly, similar to lists and vectors you can add a comment attribute to a list.

5.3.4 Subsetting Matrices

To subset matrices we use the [ operator; however, since matrices have 2 dimensions we need to incorporate subsetting arguments for both row and column dimensions. A generic form of matrix subsetting looks like: matrix[rows, columns]. We can illustrate with matrix m2:

col_1 col_2 col_3
row_1 1 5 9
row_2 2 6 10
row_3 3 7 11
row_4 4 8 12

By using different values in the rows and columns argument of m2[rows, columns], we can subset m2 in multiple ways.

col_1 col_2 col_3
row_1 1 5 9
row_2 2 6 10
col_1 col_3
row_1 1 9
row_2 2 10
row_3 3 11
row_4 4 12
col_1 col_3
row_1 1 9
row_2 2 10
col_1 col_3
row_1 1 9
row_2 2 10
row_4 4 12
col_1 col_2 col_3
row_1 1 5 9
row_3 3 7 11

Note that subsetting matrices with the [ operator will simplify11 the results to the lowest possible dimension. To avoid this you can introduce the drop = FALSE argument:

col_2
row_1 5
row_2 6
row_3 7
row_4 8

5.3.5 Numerical matrix operations

Matrix operations that are often of interest include

+ Returning the diagonal elements of a matrix

+ Computing the determinant of a matrix

+ Finding the inverse of a matrix

+ Finding the transpose of a matrix

+ Computing the Eigenvalues and Eigenvectors
  • To demonstrate matrix operations, let’s first create a \(5 \times 5\) matrix of random integers in \([1,50]\)
5 34 29 30 23
15 31 50 3 39
47 12 40 14 28
9 43 10 8 11
36 1 35 38 4
-0.0117865 -0.0156187 0.0265303 0.0132227 -0.0020199
-0.0109683 0.0046604 -0.0106166 0.0314014 0.0055918
-0.0367773 0.0462365 -0.0424756 0.0066786 0.0396274
0.0392887 -0.0248148 0.0087925 -0.0155665 -0.0027047
0.0573799 -0.0294248 0.0520145 -0.0374106 -0.0542642
5 15 47 9 36
34 31 12 43 1
29 50 40 10 35
30 3 14 8 38
23 39 28 11 4


  1. Its important to understand the difference between simplifying and preserving subsetting. Simplifying subsets returns the simplest possible data structure that can represent the output. Preserving subsets keeps the structure of the output the same as the input. See Hadley Wickham’s section on Simplifying vs. Preserving Subsetting to learn more.