7.1 Introduction to Matrices

In R, a matrix is actually an atomic vector—it can only hold one type of element—but with two extra attributes:

  • a certain number of rows, and
  • a certain number of columns.

One way to create is matrix is to take a vector and give it those two extra attributes, via the matrix() function. Here is an example:

numbers <- 1:24  # this is an ordinary atomic vector
numbersMat <- matrix(numbers, nrow = 6, ncol = 4)  # make a matrix
numbersMat
##      [,1] [,2] [,3] [,4]
## [1,]    1    7   13   19
## [2,]    2    8   14   20
## [3,]    3    9   15   21
## [4,]    4   10   16   22
## [5,]    5   11   17   23
## [6,]    6   12   18   24

Of course if you are making a matrix out of 24 numbers and you know that it’s going to have 6 rows, then you know it must have 4 columns. Similarly, if you know the number of columns then the number of rows is determined. Hence you could have constructed the matrix with just one of the row or column arguments, like this:

numbersMat <- matrix(numbers, nrow = 6)

Notice that the numbers went down the first column, then down the second, and so on. If you would rather fill up the matrix row-by-row, then set the byrow parameter, which is FALSE by default, to TRUE:

matrix(numbers, nrow = 6, byrow = TRUE)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
## [4,]   13   14   15   16
## [5,]   17   18   19   20
## [6,]   21   22   23   24

Sometimes we like to give names to our rows, or to our columns, or even to both:

rownames(numbersMat) <- letters[1:6]
colnames(numbersMat) <- LETTERS[1:4]
numbersMat
##   A  B  C  D
## a 1  7 13 19
## b 2  8 14 20
## c 3  9 15 21
## d 4 10 16 22
## e 5 11 17 23
## f 6 12 18 24

Matrices don’t have to be numerical. They can be character or logical matrices as well:

creatures <- c("Dorothy", "Lion", "Scarecrow", "Oz",
               "Toto", "Boq")
matrix(creatures, ncol = 2)
##      [,1]        [,2]  
## [1,] "Dorothy"   "Oz"  
## [2,] "Lion"      "Toto"
## [3,] "Scarecrow" "Boq"

If you have to spread out the elements of a matrix into a one-dimensional vector, you can do so:

as.vector(numbersMat)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24

7.1.1 Practice Exercises

Let’s work with the following vector:

dozen <- letters[1:12]
  1. Starting with dozen write a command that produces the following matrix:

    ##      [,1] [,2] [,3] [,4]
    ## [1,] "a"  "d"  "g"  "j" 
    ## [2,] "b"  "e"  "h"  "k" 
    ## [3,] "c"  "f"  "i"  "l"
  2. Starting with dozen write a command that produces the following matrix:

    ##      [,1] [,2] [,3]
    ## [1,] "a"  "e"  "i" 
    ## [2,] "b"  "f"  "j" 
    ## [3,] "c"  "g"  "k" 
    ## [4,] "d"  "h"  "l"
  3. Starting with dozen write a command that produces the following matrix:

    ##      [,1] [,2] [,3]
    ## [1,] "a"  "b"  "c" 
    ## [2,] "d"  "e"  "f" 
    ## [3,] "g"  "h"  "i" 
    ## [4,] "j"  "k"  "l"
  4. Starting with dozen, write commands that produce the following matrix:

    ##    c1  c2  c3 
    ## r1 "a" "b" "c"
    ## r2 "d" "e" "f"
    ## r3 "g" "h" "i"
    ## r4 "j" "k" "l"
  5. Suppose you make the following matrix:

    smallMat <- matrix(c(8, 5, 3, 4), nrow =2)
    smallMat
    ##      [,1] [,2]
    ## [1,]    8    3
    ## [2,]    5    4

    What’s a one-line command to get the folowing vector from smallMat?

    ## [1] 8 5 3 4
  6. nrow() is a function that, when given a matrix, will tell you the number of rows in that matrix. Write a one-line command to find the number of rows in a matrix called mysteryMat.

  7. ncol() is a function that, when given a matrix, will tell you the number of rows in that matrix. Write a one-line command to find the number of columns in a matrix called mysteryMat.

7.1.2 Solutions to Practice Exercises

  1. Here’s one way to do it:

    matrix(dozen, nrow = 3)

    Here’s another way:

    matrix(dozen, ncol = 4)
  2. Here’s one way to do it:

    matrix(dozen, nrow = 4)
  3. Here’s one way to do it:

    matrix(dozen, nrow = 4, byrow = TRUE)
  4. Here’s one way to do it:

    answerMatrix <- matrix(dozen, nrow = 4, byrow = TRUE)
    rownames(answerMatrix) <- c("r1", "r2", "r3", "r4")
    colnames(answerMatrix) <- c("c1", "c2", "c3")
    answerMatrix
  5. Here’s how:

    as.vector(smallMat)
  6. The command nrow(mysteryMat) will work.

  7. The command ncol(mysteryMat) will work.