(Tutorial) Matrices in R - DataCamp
(Tutorial) Matrices in R - DataCamp
(Tutorial) Matrices in R - DataCamp
Olivia Smith
May 11th, 2020
R PROGRAMMING
Matrices in R
Learn all about R's matrix, naming rows and columns, accessing
elements also with computation like addition, subtraction,
multiplication, and division.
Matrices are the objects which are elements are represented in a two-dimensional
structure where mostly the numeric elements are present for doing various computation.
The above matrix shows that there are 3 rows and 3 columns.
https://www.datacamp.com/community/tutorials/matrices-in-r 1/7
1/1/2021 (Tutorial) Matrices in R - DataCamp
1. value - speci es the vector input, which is each entry speci es the elements in a
matrix.
4. byrow - The boolean value consists of either TRUE or FALSE. If TRUE, the elements of
the matrix are arranged in the row, whereas FALSE will arrange the element by column-
wise.
5. dimnames - speci es the name given to each element of rows and columns of a matrix.
Let's see an example below where 'mat' is the name of a matrix with vector input inside c
consisting of values as 8,4,5,6,7,9. It also contains the number of rows, i.e., 'nrow' as two
and 'ncol' as three, which means the dimension of the matrix is 2 * 3. Also, the elements of
the matrix are arranged by row, which means the entry of vector input is lled up row-
wise.
The output of the above code is shown below, where there are three columns and two
rows where the elements are lled up with row-wise.
Let's create a matrix where there are elements from 1 to 9, with the number of rows to be
3. Also, the rows and columns are named with the vector input containing a list from r1 to
r3 and c1 to c3.
https://www.datacamp.com/community/tutorials/matrices-in-r 2/7
1/1/2021 (Tutorial) Matrices in R - DataCamp
c1 c2 c3
r1 1 4 7
r2 2 5 8
r3 3 6 9
The above output shows that there are elements from 1 to 9 lled through column-wise.
Also, the rows and columns are seen by labeling from r1 to r3 and c1 to c3.
c1 c2 c3
r1 1 4 7
r2 2 5 8
r3 3 6 9
For accessing the above elements of a matrix individually, you need to pass a matrix name
with a bracket consisting of rows and columns inside.
To access all the rst column of the matrix, i.e., [1 2 3] following syntax is used.
mat[,1]
To access all the second row of the matrix, i.e., [2 5 8] following syntax is used.
mat[2,]
Condition: The condition for matrix addition and subtraction is the matrices need to have
the same dimension where the number of rows and columns need to be identical. For
https://www.datacamp.com/community/tutorials/matrices-in-r 3/7
1/1/2021 (Tutorial) Matrices in R - DataCamp
example
The below matrices mat1 and mat2 consist of dimension 3 * 3, where there are 3 rows and
columns where the row number and column number in both matrices are equal.
The above matrices are printed out where each entry or elements are printed out where
the entries are lled by column-wise.
Addition
[,1] [,2] [,3]
[1,] 55 85 115
[2,] 65 95 125
[3,] 75 105 135
Subtraction
https://www.datacamp.com/community/tutorials/matrices-in-r 4/7
1/1/2021 (Tutorial) Matrices in R - DataCamp
The above output from matrix addition and subtraction is carried where each element of
both matrices get added or subtracted. For example, in matrix addition, above the entries
with row 1 and column 1, which is 5 in the mat1, gets added to the entries with row 1 and
column 1 in the mat2. As a result of it gets output 55. Similarly, all the entries follow a
similar process in addition and subtraction to get the above result.
Condition: The condition for matrix multiplication and division is the number of rows and
columns need to be similar. In other words, the dimension needs to be equal.
Matrix Multiplication
Let's see an example below where two matrices named as mat1 and mat2 are taken where
a number of rows as three and columns as two are the same. There are six elements in
matrices lled in column-wise, and the dimension of each matrix is 3 * 2.
[,1] [,2]
[1,] 2 8
[2,] 4 10
[3,] 6 12
[,1] [,2]
[1,] 14 20
[2,] 16 22
https://www.datacamp.com/community/tutorials/matrices-in-r 5/7
1/1/2021 (Tutorial) Matrices in R - DataCamp
[3,] 18 24
-----------------
Multiplication
[,1] [,2]
[1,] 28 160
[2,] 64 220
[3,] 108 288
The output from matrix multiplication above shows that each element from mat1 and
mat2 are multiplied with each other. For example, 'mat1[1][1]', which is 2 gets multiplied to
'mat2[1][1]', which is 14, to get the result as 28. Similarly, the process is followed for every
element in both matrices and nally in 'mat1[3][2]', which is 12 and mat2[3][2]' which is 24
to get the result as 288.
Matrix Division
Let's see an example below where two matrices named as mat1 and mat2 are taken where
a number of rows as three and columns as two are the same. There are six elements in
matrices lled in column-wise, and the dimension of each matrix is 3 * 2.
[,1] [,2]
[1,] 2 8
[2,] 4 10
[3,] 6 12
[,1] [,2]
[1,] 14 20
[2,] 16 22
[3,] 18 24
--------------
Division
https://www.datacamp.com/community/tutorials/matrices-in-r 6/7
1/1/2021 (Tutorial) Matrices in R - DataCamp
[,1] [,2]
[1,] 0.1428571 0.4000000
[2,] 0.2500000 0.4545455
[3,] 0.3333333 0.5000000
The output from the matrix division above shows that each element from mat1 and mat2
are divided with each other. For example, 'mat1[1][1]', which is two gets divided by 'mat2[1]
[1]', which is 14 , to get the result as 0.1428571. Similarly, the process is followed for every
element in both matrices and nally in 'mat1[3][2]', which is 12 and mat2[3][2]' which is 24
to get the result as 0.5000000.
Congratulations
Congratulations, you have made it to the end of this tutorial!
In this tutorial, you'll learn all about R's matrix, naming rows and columns, accessing
elements also with computation like addition, subtraction, multiplication, and division.
If you would like to learn more about R, take DataCamp's Introduction to R course.
16 0
Subscribe to RSS
https://www.datacamp.com/community/tutorials/matrices-in-r 7/7