A matrix is a vector with two additional attributes: the number of rows and the number of columns. Since matrices are vectors, they also have modes, such as numeric and character. (On the other hand, vectors are not one-column or one-row matrices.)
Matrices are special cases of a more general R type of object: arrays. Arrays can be multidimensional. For example, a three-dimensional array would consist of rows, columns, and layers, not just rows and columns as in the matrix case. Most of this chapter will concern matrices, but we will briefly discuss higher-dimensional arrays in the final section.
Much of R’s power comes from the various operations you can perform on matrices. We’ll cover these operations in this chapter, especially those analogous to vector subsetting and vectorization.
Matrix row and column subscripts begin with 1. For example, the upper-left corner of the matrix a
is denoted a[1,1]
. The internal storage of a matrix is in column-major order, meaning that first all of column 1 is stored, then all of column 2, and so on, as you saw in Section 2.1.3.
One way to create a matrix is by using the matrix()
function:
> y <- matrix(c(1,2,3,4),nrow=2,ncol=2) > y [,1] [,2] [1,] 1 3 [2,] 2 4
Here, we concatenate what we intend as the first column, the numbers 1 and 2, with what we intend as the second column, 3 and 4. So, our data is (1,2,3,4)
. Next, we specify the number of rows and columns. The fact that R uses column-major order then determines where these four numbers are put within the matrix.
Since we specified the matrix entries in the preceding example, and there were four of them, we did not need to specify both ncol
and nrow
; just nrow
or ncol
would have been enough. Having four elements in all, in two rows, implies two columns:
> y <- matrix(c(1,2,3,4),nrow=2) > y [,1] [,2] [1,] 1 3 [2,] 2 4
Note that when we then print out y
, R shows us its notation for rows and columns. For instance, [,2]
means the entirety of column 2, as can be seen in this check:
> y[,2] [1] 3 4
Another way to build y
is to specify elements individually:
> y <- matrix(nrow=2,ncol=2) > y[1,1] <- 1 > y[2,1] <- 2 > y[1,2] <- 3 > y[2,2] <- 4 > y [,1] [,2] [1,] 1 3 [2,] 2 4
Note that we do need to warn R ahead of time that y
will be a matrix and give the number of rows and columns.
Though internal storage of a matrix is in column-major order, you can set the byrow
argument in matrix()
to true to indicate that the data is coming in row-major order. Here’s an example of using byrow
:
> m <- matrix(c(1,2,3,4,5,6),nrow=2,byrow=T) > m [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6
Note that the matrix is still stored in column-major order. The byrow
argument enabled only our input to come in row-major form. This may be more convenient if you are reading from a data file organized that way, for example.