Matrices In R

Introduction

 
A matrix is a data set represented in a two-dimensional rectangular form. All the columns of the matrix should be of the same data type and the same length. It is similar to a vector, but it comes with the dimension attribute. In this article I am going to demonstrate how to create a matrix and perform various operations on a matrix.
 

Creation of matrices

 
To create a matrix in R, we use a vector input to the matrix function. To create a matrix use the following syntax,
  1. my_matrix <- matrix(vector, nrow=r, ncol=c, byrow=FALSE, dimnames=list(char_vector_rownames, char_vector_colnames))  
A byRow=True parameter indicates that the matrix should be filled by rows. A byRow=FALSE parameter indicates that the matrix should be filled by columns.
 
The dimnames will provide optional labels for both the rows and the columns. We should also pass the right value to the nrow and ncol arguments.
 
It is not necessary for us to pass values for both the nrow and ncol arguments. If you pass a value for one of these, the value of the other one will be inferred from the length of the data.
 
Here is an example,
 
M = matrix( c('x','x','y','z','y','x'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
 
The program should give you the following output,
  1. > M = matrix( c('x','x','y','z','y','x'), nrow = 2, ncol = 3, byrow = TRUE)  
  2. print(M)  
 
 
Here is another example, matrix(1:12, nrow = 4, ncol = 3). This generates the following matrix,
  1. > matrix(1:12, nrow = 4, ncol = 3)  
 
 
We can modify it to the following: matrix(1:12, nrow = 4). Even though we omitted the ncol attribute, the same matrix has been generated. The numbers have been filled by columns.
 

byrow property

 
If we need them to be filled by row, we can set the byrow property to TRUE as in our first example. This is demonstrated below,
  1. > matrix(1:12, nrow=3, byrow=TRUE)  
This will return the following,
 
 
 
We may fill the matrix row-wise, but internally, the matrix is stored in column-major order.
 

Assign column and row name

 
We can assign names to the columns of the matrix during the creation of the matrix. We only have to pass a 2 element list to the dimnames argument.
 
Consider the example given below,
 
x <- matrix(1:12, nrow = 4, dimnames = list(c("W","X","Y" ,"Z"), c("A","B","C")))
 
The code should return the following,
  1. > x <- matrix(1:12, nrow = 4, dimnames = list(c("W","X","Y" ,"Z"), c("A","B","C")))  
  2. print(x)  
 
 

Accessing column and row names

 
To change or access the names, we can use two helpful functions, the colnames() and rownames() functions. The name of the matrix should be passed to the function as the argument.
 
colnames(x)
  1. > colnames(x)  
  2. [1"A" "B" "C"  
The names of the columns have been defined. Let us see the names of the rows.
 
rownames(x)
  1. > rownames(x)  
  2. [1"W" "X" "Y" "Z"  
 
 
The names of the matrix rows have been returned.
 

cbind() and rbind() functions

 
We can also create a matrix by use of the cbind() and rbind() functions for column bind and row bind respectively. Let us give examples of these.
 
cbind 
  1. > cbind(c(169),c(358))  
 
The matrix has been created.
 
rbind
  1. > rbind(c(169),c(358))  
 
 
The matrix has been created.
 

Accessing elements of a matrix

 
The elements of a matrix can be accessed by use of a square bracket [] indexing method. We can access the elements using the format var[row, column], where the rows and column are vectors.
 
R allows us to specify the row numbers and column numbers as vectors then use it for indexing. If any of the fields inside the brackets are found blank, all will be selected.
 
To specify the rows and columns that are to be executed, we can use negative integers.
 
Consider the example given below.
 
First, let us create a matrix from a vector,
  1. > x <- c(136450)  
  2. > dim(x) <- c(2,3)  
  3. print(x)  
  4.      [,1] [,2] [,3]  
  5. [1,]    1    6    5  
  6. [2,]    3    4    0  
Here, we need to access the rows 1 and 2 and columns 2 and 3 of the matrix x,
 
x[c(1, 2),c(2, 3)]
 
The above will return the following,
  1. > x[c(12),c(23)]  
  2.      [,1] [,2]  
  3. [1,]    6    5  
  4. [2,]    4    0  
If the column space is left blank, then the entire columns will be selected as shown below,
 
x[c(1, 2),]
 
The command will return the following,
  1. > x[c(12),]  
  2.      [,1] [,2] [,3]  
  3. [1,]    1    6    5  
  4. [2,]    3    4    0  
If the row and column fields are left empty, the entire matrix will be selected,
 
x[,]
 
This will return the following,
  1. > x[,]  
  2.      [,1] [,2] [,3]  
  3. [1,]    1    6    5  
  4. [2,]    3    4    0  
We can select all rows except the first one as follows,
  1. > x[-1,]  
  2. [13 4 0  
Note that after indexing, if the matrix returns a row matrix or column matrix, the displayed result is a vector. You can see this by running the following commands, the commands will run as follows,
  1. > x[1,]  
  2. [11 6 5  
  3. class(x[1,])  
  4. [1"numeric"  
 
 
To prevent this behavior from occurring, we can use the attribute drop = FALSE when indexing. This is demonstrated below,
 
x[1,,drop=FALSE]
 
The command will return the following,
  1. > x[1,,drop=FALSE]  
  2.      [,1] [,2] [,3]  
  3. [1,]    1    6    5  
  4. >  
The output shown above is a matrix rather than a vector. You can confirm this by running the following command,
 
class(x[1,,drop=FALSE])
 
It should run as follows,
  1. class(x[1,,drop=FALSE])  
  2. [1"matrix" "array"   
  3. >  

Modify the elements of a matrix

 
Whenever you need to modify the elements of a matrix, you can use the necessary methods to access the elements and change them as required.
 
Suppose we have the following matrix x,
  1. >  x <- c(136450)  
  2. > dim(x) <- c(2,3)  
  3. >   
  4. > x  
  5.      [,1] [,2] [,3]  
  6. [1,]    1    6    5  
  7. [2,]    3    4    0  
  8. >  
We can modify a single element using the following command,
 
x[2,2] <- 10; x
 
The command should execute as follows,
  1. > x[2,2] <- 10; x  
  2.      [,1] [,2] [,3]  
  3. [1,]    1    6    5  
  4. [2,]    3   10    0  
 
 
As we can see from the above output, element in second row and second column has been changed to 10.
 

Summary

 
In this article, I demonstrated how to create a matrix and perform various operations on a matrix such as to assign column and row name to a matrix, accessing column and row names, accessing elements of a matrix and modifying the elements of a matrix. Proper coding snippets along with output have been provided. 


Similar Articles