Beyond Vectors: Introduction to Matrices and Arrays in R Part IV

In this post, we will look at how to rename the rows and columns in a matrix. We will also examined how to do the following

  • Name rows and columns in matrices
  • Make an array
  • Basic math in matrices & arrays

Naming Rows and Columns

Renaming rows and columns has a practical use. It allows people to provide meaning and or context to the data they are interpreting. In the example below, we will rename the rows and the columns of the basketball players so that it contains their names as well as what game.

> points.of.James
[1] 12 15 30 25 23 32
> points.of.Kevin
[1] 20 19 25 30 31 22
> points.team <- rbind(points.of.James, points.of.Kevin)
> points.team
                [,1] [,2] [,3] [,4] [,5] [,6]
points.of.James   12   15   30   25   23   32
points.of.Kevin   20   19   25   30   31   22
> rownames(points.team) <- c("James", "Kevin")
> points.team
      [,1] [,2] [,3] [,4] [,5] [,6]
James   12   15   30   25   23   32
Kevin   20   19   25   30   31   22
> colnames(points.team) <- c("1st", "2nd", "3rd", "4th", "5th", "6th")
> points.team
      1st 2nd 3rd 4th 5th 6th
James  12  15  30  25  23  32
Kevin  20  19  25  30  31  22

.Here is what’s going on

  1. We make the variables ‘point.of.James’ and ‘point.of.Kevin’ and display them
  2. We combine ‘point.of.James’ and ‘point.of.Kevin’ into a matrix using the ‘rbind’ function and assign this to the variable ‘points.team’
  3. We then display ‘points.team’
  4. We then rename the rows using the ‘rownames’ function. We replace ‘point.of.James’ and ‘point.of.Kevin’ with ‘James’ and “Kevin’ in the rows. We then display this change in the matrix.
  5. Next we rename 1,2,3,4,5,6 with 1st, 2nd, 3rd, 4th, 5th, & 6th. using the ‘colnames’ function.
  6. Lastly, we display the finished table.

Making Arrays

A vector has one dimension (row), a matrix has two dimensions (row & column), and an array has three or more dimensions (length, width, & height for example). We are not going to cover arrays extensively because it is hard to envision data beyond 3 dimensions. In addition, please remember that all the rules and tricks for vectors and matrices apply towards an array. Below is an example of how to make an array

> array1 <-array(1:48, dim=c(6, 8, 4))
> array1
, , 1

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    7   13   19   25   31   37   43
[2,]    2    8   14   20   26   32   38   44
[3,]    3    9   15   21   27   33   39   45
[4,]    4   10   16   22   28   34   40   46
[5,]    5   11   17   23   29   35   41   47
[6,]    6   12   18   24   30   36   42   48

, , 2

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    7   13   19   25   31   37   43
[2,]    2    8   14   20   26   32   38   44
[3,]    3    9   15   21   27   33   39   45
[4,]    4   10   16   22   28   34   40   46
[5,]    5   11   17   23   29   35   41   47
[6,]    6   12   18   24   30   36   42   48

, , 3

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    7   13   19   25   31   37   43
[2,]    2    8   14   20   26   32   38   44
[3,]    3    9   15   21   27   33   39   45
[4,]    4   10   16   22   28   34   40   46
[5,]    5   11   17   23   29   35   41   47
[6,]    6   12   18   24   30   36   42   48

, , 4

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    7   13   19   25   31   37   43
[2,]    2    8   14   20   26   32   38   44
[3,]    3    9   15   21   27   33   39   45
[4,]    4   10   16   22   28   34   40   46
[5,]    5   11   17   23   29   35   41   47
[6,]    6   12   18   24   30   36   42   48

Here is what is happening

  1. We created ‘array1’ and assigned an array to it that contains numbers 1 to 48 and has 6 rows, 8 columns, and 4 dimensions.
  2. When then have R display the results

To locate values in specific indices you have to add an additional number to the address. For example, if you are looking for 1,1,1 you would go to the first dimension, first row, and first column. Also do not forget how extracting rows and columns re-shuffles the numbering of the remaining rows and columns.

Basic Math in Matrices and Arrays

You can change all the values in a matrix and array. For example, let us say we want to add four points to every game that Kevin and James played. We can do this by using the following code.

> points.team
      1st 2nd 3rd 4th 5th 6th
James  12  15  30  25  23  32
Kevin  20  19  25  30  31  22
> new.points.tream <- points.team+4
> new.points.tream
      1st 2nd 3rd 4th 5th 6th
James  16  19  34  29  27  36
Kevin  24  23  29  34  35  26

What happening

  1. We redisplayed the variable “points. team’
  2. We then create a new variable called ‘new.points.team’ and we told R to add 4 points to every value in the variable ‘points.team’
  3. We display the new table. A closer look will show how every value is 4 points higher in the new table than the results of

Conclusions

This conclude the examinations on matrices and arrays.

3 thoughts on “Beyond Vectors: Introduction to Matrices and Arrays in R Part IV

  1. Pingback: Data Frames in R: Part I | educationalresearchtechniques

  2. Pingback: Data Frames in R: Part III | educationalresearchtechniques

  3. Pingback: Beyond Vectors: Introduction to Matrices and Ar...

Leave a Reply