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

In this post, we will continue to explore the basic features of matrices. Specifically, we will look at how to replace values in a matrix and how to combine vectors into a matrix.

Replacing Values in a Matrix

There are times when we might input the wrong value into a matrix. The hard way to deal with this problem is to remake the entire matrix. The easy way is to only replace the incorrect value. You can change values by individual index, by row or column or by even importing another matrix (we will not covering replaces values in a matrix with another matrix). Below are examples

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

In this first example we wanted to replace the value in index 1,1 with the number 4 here is what happened.

  1. We created the matrix ‘matrix1’ using numbers 1-20 (1:20) with 4 columns (ncol = 4).
  2. We type ‘matrix1’ so R displays it. The purpose of this is so that we can compare the original matrix with the modification.
  3. We than type in ‘matrix1’ into R and subset row 1 column 1 using brackets and we assigned the number 4 to row 1 column 1.
  4. We then type ‘matrix1’ to show the modified matrix

If you look carefully at 1,1 in the matrix you will that the value in this index has been changed from 1 to 4.

Below is an example of replace an entire row. This technique can also be used for replacing a column

> 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
> matrix1[2, ] <- c(2,3)
> matrix1
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    3    2    3
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

Here is what happened

  1. We created the matrix ‘matrix1’ and displayed it as a reference
  2. We then subsetted the second row of ‘matrix1’ and told are to put in that row the numbers 2 and 3
  3. We then displayed the modified matrix

If you look carefully, you will see that in the second row we now have the pattern 2,3.  When you tell R to replaces values using a pattern, R will continue to repeat the pattern until the row is filled. Even though we only told R to use two numbers (2 and 3) R filled all four columns in the second row with the pattern 2,3.

Combining Vectors into a Matrix

You can combine two or more vectors into a matrix by using the function ‘rbind’ below is an example.

> points.of.James
[1] 12 15 30 25 23 32
> points.of.Kevin
[1] 20 19 25 30 31 22
> points.of.players <- rbind(points.of.James, points.of.Kevin)
> points.of.players
                [,1] [,2] [,3] [,4] [,5] [,6]
points.of.James   12   15   30   25   23   32
points.of.Kevin   20   19   25   30   31   22

Here is what we did

  1. We redisplayed the values of the variables ‘points.of.James’ and ‘points.of.Kevin’
  2. When then created the variable ‘points.of.players’ and used the ‘rbind’ function to combine the values in the vectors/variables of ‘points.of.James’ and ‘points.of.Kevin’
  3. We then display the results

In a future post, we will examine how to rename the rows and columns so that they provide critical information for people who may be trying to interpret the information.

Advertisements

2 thoughts on “Beyond Vectors: Introduction to Matrices and Arrays in R Part III

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

  2. Pingback: Understanding Lists 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