In R programming, a Matrix is an object with elements arranged as a two-dimensional array like a table. An R matrix can contain elements of only the same atomic types.
In data analytics or data processing, we mostly use Matrix with the numeric datatype. In this tutorial, we will deal with Matrix containing numbers.
Create a Matrix in R
matrix() function is used to create a Matrix in R. Following is the syntax of matrix() function.
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
where
datais the input vector which becomes the data elements of the matrix.NAis the default value.nrowdenotes the number of rows to be created.1is the default value.ncolspecifies the number of columns to be created.1is the default value.- If
byrowis TRUE then the input vector elements are arranged by row. Ifbyrowis FALSE, the input vector elements are arranged by column. dimnameis the names assigned to the rows and columns like in an Excel file.
Example to create an R matrix
In this example, we will create a matrix with a vector of numbers as input. A vector of numbers is of fixed length. If we do not specify any number for rows or columns, the vector of numbers is taken as a single column.
> M <- matrix(c(1:12)) > M [,1][1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 [11,] 11 [12,] 12
Let us specify a number for nrow, i.e., number of rows.
> M <- matrix(c(1:12), nrow=3) > M [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12
Total number of elements in the Vector = 12.
So, when we specified number of rows = 3,
Number of columns = (total number of elements)/nrow = 12/3 = 4
Instead of specifying nrow, we can specify ncol while creating an R matrix.
> M <- matrix(c(1:12), ncol=3) > M [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12
The same explanation as of nrow holds for ncol as well.
If you specify a value for nrow or ncol which does not exactly divide the total number of elements, you will get an error saying that “data length is not a sub-multiple or multiple of the number of columns”
> M <- matrix(c(1:12), ncol=5) Warning message: In matrix(c(1:12), ncol = 5) : data length [12] is not a sub-multiple or multiple of the number of columns [5] >
Now let us see how byrow affects the creation of the Matrix with this example.
> M1 <- matrix(c(1:12), ncol=3, byrow=FALSE) > M1 [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 > M2 <- matrix(c(1:12), ncol=3, byrow=TRUE) > M2 [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12>
While creating matrix M1, byrow=FALSE. Which means the vector of numbers are arranged by column. The sequence is column wise.
While creating matrix M2, byrow=TRUE. Which means the vector of numbers are arranged by row. The sequence is row wise.
Now its time to know how to use dimnames. We can specify both row names and column names. So we will take two vectors, one for rows and one for columns.
> rownames = c('r1','r2','r3','r4')
> colnames = c('c1','c2','c3')
> names = list(rownames, colnames)
> M1 = matrix(c(1:12), nrow=4, dimnames=names)
c1 c2 c3
r1 1 5 9
r2 2 6 10
r3 3 7 11
r4 4 8 12
Access Elements of R Matrix
You can access Elements of an R matrix using the column and row index of the element.
MatrixName[row_index, column_index]
Let us pick element at row=3 and column=2.
> M1 [,1] [,2] [,3][1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 > M1[3,2] [1] 7
If row index is not mentioned and column index is mentioned, the whole column is returned.
> M1 [,1] [,2] [,3][1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 > M1[,2] [1] 5 6 7 8
M1[3,] selects thrid row of the matrix M1.
R Matrix Addition
In R, we can add two Matrix. To add two Matrix, use addition (+) operator. The result is a matrix with the sum of the two operand Matrix.
When performing addition of two matrix, the size of two matrix, i.e., number of rows and columns should be same.
> M1
[,1] [,2] [,3][1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> M2
[,1] [,2] [,3][1,] 5 9 13
[2,] 6 10 14
[3,] 7 11 15
[4,] 8 12 16
> M3 = M1 + M2
> M3
[,1] [,2] [,3][1,] 6 14 22
[2,] 8 16 24
[3,] 10 18 26
[4,] 12 20 28
R Matrix Subtraction
In R, we can subtract a Matrix from other. To subtract Matrix, use subtraction (-) operator. The result is a matrix with the difference between first and second matrix.
When performing subtraction of two matrix, the size of two matrix, i.e., number of rows and columns should be same.
> M2
[,1] [,2] [,3][1,] 5 9 13
[2,] 6 10 14
[3,] 7 11 15
[4,] 8 12 16
> M1
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> M3 = M2 - M1
> M3
[,1] [,2] [,3]
[1,] 4 4 4
[2,] 4 4 4
[3,] 4 4 4
[4,] 4 4 4
>
R Matrix Multiplication
To multiply elements of a matrix with the corresponding elements of other matrix, use multiplication (*) operator. The multiplication happens only between the (i,j) of first matrix and (i,j) of second matrix.
> M1
[,1] [,2] [,3][1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> M2
[,1] [,2] [,3]
[1,] 5 9 13
[2,] 6 10 14
[3,] 7 11 15
[4,] 8 12 16
> M3 = M1*M2
> M3
[,1] [,2] [,3][1,] 5 45 117
[2,] 12 60 140
[3,] 21 77 165
[4,] 32 96 192>
R Matrix Division
To divide elements of a matrix with the corresponding elements of other matrix, use division (/) operator. The multiplication happens only between the (i,j) of first matrix and (i,j) of second matrix.
> M1
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> M2
[,1] [,2] [,3][1,] 5 9 13
[2,] 6 10 14
[3,] 7 11 15
[4,] 8 12 16
> M3 = M2/M1
> M3
[,1] [,2] [,3]
[1,] 5.000000 1.800000 1.444444
[2,] 3.000000 1.666667 1.400000
[3,] 2.333333 1.571429 1.363636
[4,] 2.000000 1.500000 1.333333>
