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

In this post, we will continue to explore the different actions that can be performed when using matrices and arrays. In particular, we will look at how to manipulate the values within a matrix.

Manipulating Values in a Matrix

Before manipulating values it is important to understand what an index is. An index or Indices, which is the plural form, is the address f a value within a matrix. There are two numbers involved in an index. The first number represents the row that the value is in and the second number represents the column. Again this is similar to Microsoft excel and its system for giving every cell an address.

Below is an example to help make this clearer

> matrix1<- matrix(1:20, ncol=4)
> matrix1
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

Here is what we did,

  1. We made a variable called ‘matrix1’ and this variable has a matrix of 20 consecutive numbers (1:20) with four columns (ncol = 4)
  2. We then print ‘matrix1’ in R

Now for a simple quiz

What value is in row 3 column 2?*

What value is in row 5 column 4?**

The answers are at the bottom of the post.

In R, we do not say ‘row 3’ and ‘column 2’. Instead we would simply say 3, 2 or 5, 4 in the code.

Extracting Values

With our knowledge of indices we can now extract or removes values from a matrix. For example, below is a way to extract and only look at the first two rows and columns two and three of our matrix. In each example, I will display the original matrix and then the extracted matrix so you can compare the differences.

> matrix1
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> matrix1[1:2, 2:3]
     [,1] [,2]
[1,]    6   11
[2,]    7   12

Here is what happen

  1. We printed matrix1 as a reference for comparison
  2. We then extracted the first two rows (1:2) and the second and third columns (2:3) of ‘matrix1’ using brackets
  3. R then prints the results

Notice how R renames the rows and columns. Rows 1 and 2 are still rows 1 and 2 because we extracted them. However, what use to be rows 2 and 3 has been renamed rows 1 and 2 in the extracted matrix. This can get really confusing so be careful.

If you want to extract an entire row you do not specify the column as in the example below

> matrix1
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> matrix1[2:3,]
     [,1] [,2] [,3] [,4]
[1,]    2    7   12   17
[2,]    3    8   13   18

Here is what happening.

  1. We print ‘matrix1’ as a reference for comparison
  2. We extract from ‘matrix1’ rows 2 and 3 (2:3) using brackets. Notice that we put a comma after 2:3. This tells R to take the whole row.
  3. R prints the results. Notice what was once rows 2 and 3 is now rows 1 and 2.

You can also remove rows and columns in a matrix as in the example that follows

> matrix1
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> matrix1[-2,-3]
     [,1] [,2] [,3]
[1,]    1    6   16
[2,]    3    8   18
[3,]    4    9   19
[4,]    5   10   20

Here is what is going on

  1. We print ‘matrix1’ as a reference.
  2. We then tell R to remove row 2 and column 3 (-2, -3). The removal of a row and or column is indicated through using a negative – sign.
  3. R prints the results. Notice that there are now 4 rows and 3 columns because we remove one row and one column. In addition, don’t forget that the numbers and columns have been renumbered

ANSWERS TO QUIZ

*8

**20

Advertisements

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

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

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

  3. Pingback: Apply Functions in R | educationalresearchtechniques

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s