Welcome to the spoken tutorial on Creating Matrices using Data Frames.
In this tutorial, we will learn how to:
Convert a data frame into a matrix
Create a matrix in R
Perform basic operations on matrices
To understand this tutorial, you should know,
Data frames and Vectors in R
Basics of Matrices
R script in RStudio
How to set working directory in RStudio
If not, please locate the relevant tutorials on R on this website.
This tutorial is recorded on
Ubuntu Linux OS version 16.04
R version 3.4.4
RStudio version 1.1.456
Install R version 3.2.0 or higher.
For this tutorial, we will use the data frame CaptaincyData.csv and a script file myMatrix.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to Matrices folder in myProject folder on the Desktop.
I have also set Matrices folder as my Working Directory.
Let us switch to RStudio.
Open the script myMatrix.R in RStudio.
For this, click on the script myMatrix.R
Script myMatrix.R opens in RStudio.
Here, I have used rm function to remove all existing variables from memory.
This prevents errors such as use of older data.
Run this script by clicking on the Source button.
The captaincy data frame is displayed in the Source window.
We need our data in a matrix form to:
Calculate the inverse of a matrix
Perform matrix operations like addition, subtraction, multiplication, etc.
Hence, we need to learn how to convert a data frame into a matrix.
Let us get back to RStudio.
We will convert the values in first three rows of played, won and lost columns into a matrix.
We will create a subset for extracting these values.
Click on the script myMatrix.R
In the Source window, type the following command Press Enter.
I am resizing the Source window.
Press Enter to go to the next line.
Press Enter at the end of every command.
1 colon 3 indicates that the first three rows are to be extracted.
c is the concatenate vector command with names of each column within double quotes.
Save the script and execute the current line.
The variable subData appears in the Environment window.
Click on the arrow before subData to expand its contents.
Now, we will convert this subset into a matrix.
In the Source window, type matrixA then press Alt and hyphen(-)keys simultaneously.
Now type asdot matrix, within parentheses type subdata and press Enter.
as.matrix function is used to convert the subdata into a matrix.
In Source window, type print, within parentheses matrixA.
Save the script and run these two lines.
The content of matrixA is shown on the Console window.
Now let us create a 3 by 3 identity matrix.
Create a vector named values containing the row-wise values of a 3 by 3 identity matrix.
In the Source window, type the following command and press Enter.
Now we will create a variable matrixB to store the matrix created using vector values.
In the Source window, type the following command. Press Enter.
Press Enter to go to the next line.
Here, I have used matrix function with following arguments:
data of values vector
number of rows as nrow equal to 3
number of columns as ncol equal to 3
byrow equal to TRUE indicates that, data provided has to be arranged row-wise in the matrix.
Now, type print within parentheses type matrixB to show the contents of the matrix.
Save the script and run the last three lines of code.
The identity matrix is shown on the Console window.
Now let us perform some arithmetic operations on matrices.
I will resize the Source window.
We will add and subtract matrices A and B.
In the Source window, type matrixA space plus sign space matrixB.
Next, type matrixA space minus sign space matrixB
Save the script and run these two lines.
I will resize the Console window to see the output properly.
The resulting matrices are shown on the Console window.
Let us perform multiplication of individual elements of two matrices.
In the Source window, type matrixA space asterisk sign space matrixB
Save the script and run the current lines.
The resulting matrix is shown on the Console window.
Now, we will perform a true matrix multiplication with these matrices.
1st row of matrixA is multiplied by 1st column of matrixB.
Hence number of columns of matrixA should be equal to the number of rows of matrixB.
In the Source window, type matrixA space percentage sign asterisk percentage sign space matrixB.
Save the script and run the current line.
The resulting matrix is shown on the Console window.
Now let us calculate the transpose and determinant of matrixA.
In the Source window, type t and matrixA in parentheses.
Now, type det and matrixA in parentheses.
Save the script and run these two lines.
The corresponding output is shown on the Console window.
Let us summarize what we have learnt.
In this tutorial, we have learnt how to:
Convert a data frame into a matrix
Create a matrix in R
Perform basic operations on matrices
We now suggest an assignment.
Consider 2 vectors c(9,10,11,12) and c(13,14,15,16).
Create a 4 by 2 matrix from these two vectors.
For solution, please refer to the Additional materials section on this website.
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it.
We conduct workshops using Spoken Tutorials and give certificates.
Please contact us.
Please post your timed queries in this forum.
Please post your general queries in this forum.
The FOSSEE team coordinates the TBC project.
For more details, please visit these sites.
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
The script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018).
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.