5.1 Creating matrices

Matrices are created with the function matrix():

> matrix(1:12, nrow=3, ncol=4)
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

Note that in mathematics, matrices are usually denoted by a (bold) captial letter, and we will adopt this convention here.

The example above creates a 3x4 matrix with 3 rows and 4 columns, filled with the numbers from 1 through 12. Notice that the function matrix() takes a vector as input and fills it into the matrix of the specified size. By default, the matrix is filled column-by-column, but it can also be filled row-by-row by setting the argument byrow=TRUE.

> matrix(1:12, nrow=3, ncol=4, byrow=TRUE)
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12

Note that in case you provide the function matrix() with a vector, there is no need to specify both dimensions: if a matrix with 3 rows is to be constructed from 12 elements, there must be 4 columns. Hence we can use

> matrix(1:12, nrow=3)
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> matrix(1:12, ncol=4)
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

If you provide both dimensions and the length of the provided vector does not match, it will either be subset or recycled.

> matrix(1:12, nrow=2, ncol=3)
Warning in matrix(1:12, nrow = 2, ncol = 3): data length differs from size of
matrix: [12 != 2 x 3]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
> matrix(1:3, nrow=2, ncol=3)
     [,1] [,2] [,3]
[1,]    1    3    2
[2,]    2    1    3
> matrix(1, nrow=2, ncol=3)
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1

Just as for vectors, R prints a warning if the provided vector is not a multiple or submultiple of the number of required elements.

> matrix(1:10, nrow=2, ncol=3)
Warning in matrix(1:10, nrow = 2, ncol = 3): data length [10] is not a
sub-multiple or multiple of the number of columns [3]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

To check for the dimensionality of a matrix, you may use the function dim() that returns a vector of two elements, the first being the number of rows and the second the number of columns of the matrix. To directly get the number of rows or columns, use nrow()and ncol().

> A <- matrix(1:12, nrow=4)
> dim(A)
[1] 4 3
> nrow(A)
[1] 4
> ncol(A)
[1] 3

An important type of matrices in statistics are diagnoal matrices that only have non-zero values on the diagonal. You can generate such matrices easily with the function diag().

> diag(1, nrow=4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1
> diag(1:3)
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    2    0
[3,]    0    0    3

Note that the function diag() can also be used to access the diagnoal of a matrix.

> A <- matrix(1:16, nrow=4);
> A
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
> diag(A)
[1]  1  6 11 16

Since a matrix is a vector to two dimensions, we can also create matrices by combining vectors with the following functions:

cbind() bind by column rbind() bind by row

This way, each vector would be one column or row (depends on the function used).

> vector1 <- 1:8
> vector2 <- 9:16
> vector3 <- 17:24

By row

> rbind(vector1,vector2,vector3)
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
vector1    1    2    3    4    5    6    7    8
vector2    9   10   11   12   13   14   15   16
vector3   17   18   19   20   21   22   23   24

By column

> cbind(vector1,vector2,vector3)
     vector1 vector2 vector3
[1,]       1       9      17
[2,]       2      10      18
[3,]       3      11      19
[4,]       4      12      20
[5,]       5      13      21
[6,]       6      14      22
[7,]       7      15      23
[8,]       8      16      24

Finally, matrices can contain NAs.. for instance:

> vector1 <- c(NA, NA, 3)
> vector2 <- c(NA,  5,  NA)
> 
> rbind(vector1, vector2)
        [,1] [,2] [,3]
vector1   NA   NA    3
vector2   NA    5   NA

5.1.1 Exercises: Creating matrices

See Section 18.0.11 for solutions.

  1. Create a 5x10 matrix filled with 1.

  2. Create a diagonal 10x10 matrix with elements 1,2,1,2, …

  3. Create a matrix A with the two rows 1, 2, …, 10 and 10, 9, …, 1.

  4. Create the same matrix A but using rbind().

  5. Generate a vector with the numbers 1, 2, … 80 and assign it to the variable b. Now turn this vector into a matrix B with 5 columns. Finally, assign the number of rows of B to variable k.

  6. Generate a 3x3 matrix with all diagonal elements being 1 and all other elements being 2.