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,
- 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,
- > M = matrix( c('x','x','y','z','y','x'), nrow = 2, ncol = 3, byrow = TRUE)
- > print(M)
Here is another example, matrix(1:12, nrow = 4, ncol = 3). This generates the following matrix,
- > 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,
- > 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,
- > x <- matrix(1:12, nrow = 4, dimnames = list(c("W","X","Y" ,"Z"), c("A","B","C")))
- > 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)
- > colnames(x)
- [1] "A" "B" "C"
The names of the columns have been defined. Let us see the names of the rows.
rownames(x)
- > rownames(x)
- [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
- > cbind(c(1, 6, 9),c(3, 5, 8))
The matrix has been created.
rbind
- > rbind(c(1, 6, 9),c(3, 5, 8))
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,
- > x <- c(1, 3, 6, 4, 5, 0)
- > dim(x) <- c(2,3)
- > print(x)
- [,1] [,2] [,3]
- [1,] 1 6 5
- [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,
- > x[c(1, 2),c(2, 3)]
- [,1] [,2]
- [1,] 6 5
- [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,
- > x[c(1, 2),]
- [,1] [,2] [,3]
- [1,] 1 6 5
- [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,
- > x[,]
- [,1] [,2] [,3]
- [1,] 1 6 5
- [2,] 3 4 0
We can select all rows except the first one as follows,
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,
- > x[1,]
- [1] 1 6 5
- > class(x[1,])
- [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,
- > x[1,,drop=FALSE]
- [,1] [,2] [,3]
- [1,] 1 6 5
- >
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,
- > class(x[1,,drop=FALSE])
- [1] "matrix" "array"
- >
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,
- > x <- c(1, 3, 6, 4, 5, 0)
- > dim(x) <- c(2,3)
- >
- > x
- [,1] [,2] [,3]
- [1,] 1 6 5
- [2,] 3 4 0
- >
We can modify a single element using the following command,
x[2,2] <- 10; x
The command should execute as follows,
- > x[2,2] <- 10; x
- [,1] [,2] [,3]
- [1,] 1 6 5
- [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.