Welcome to the spoken tutorial on Operations on Matrices and Data Frames.
In this tutorial, we will learn how to:
Perform operations on matrices
Add rows or columns to a data frame
To understand this tutorial, you should know
Data frames and Matrices in R
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 the script file myMatrix.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to MatrixOps folder' in myProject folder on the Desktop.
I have also set MatrixOps 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.
Recall that, we had created a matrix named matrixA.
This matrix was extracted as a subset from the captaincy data frame.
We will use matrixA here also.
Run this script myMatrix.R to load the values in the Workspace.
captaincy data frame opens in the Source window.
I will resize the Source window.
Now let us learn how to find the inverse of matrixA.
Click on the script myMatrix.R
In the Source window, type solve, within parentheses matrixA.
Press Enter. Press Enter at the end of every command.
Save the script and run this line.
The inverse of matrixA in shown in the Console window.
For more information on calculating inverse of a matrix in R, please refer to the Additional materials section on this website.
Now let us calculate the sum of all the elements in matrixA.
First, we shall use nested for loops for calculating this sum.
Also, we shall estimate the time taken to calculate the sum in this way.
Let us add a comment - Calculating sum using for loop in the script.
In the Source window, type # hash space Calculating sum using for loop.
Press Enter. To calculate the time taken, we record the present time.
In the Source window, type start Time with capital T.
Then press Alt and -(hyphen) keys simultaneously.
Next, type Sys dot time followed by parentheses.
Sys.time() is used to find the absolute date-time value in the current time.
Let us initialise a variable totalSum.
In the Source window, type totalSum
then press Alt and -(hyphen) keys simultaneously. Then type zero.
This variable will store the sum of elements in matrixA.
Now, we will create two for loops to iterate through the rows and columns of the matrixA.
In the Source window, type for in parentheses i space in space 1 colon 3 space
Now type opening curly bracket. RStudio automatically adds a closing curly bracket.
Press Enter just after the opening curly bracket and type the following command.
Press Enter after the closing curly bracket of outer loop for(i in 1:3).
In the Source window, type print totalSum in parentheses.
Now, we record the current time again.
Type endTime then press Alt and -(hyphen) keys simultaneously. Type Sys dot time parentheses.
Press Enter. Type endTime space minus sign space startTime to know the total time taken. And save the script.
I am resizing the Source window.
Run the block of code from the comment line Calculating sum using for loop to the end.
The totalSum is evaluated to be 237.
The time taken to calculate the sum of elements in matrixA using for loop is approximately 8 milliseconds.
However, it may vary from system to system.
Now, let us calculate the sum of all elements in matrixA using the sum function.
We shall estimate the time taken to calculate the sum in this way also.
In the script myMatrix.R, add a comment Calculating sum using inbuilt function.
In the Source window, type # hash space Calculating sum using inbuilt function.
Now, type the following commands.
Save the script and run the block of code from the comment line Calculating sum using inbuilt function to the end.
The time taken to calculate the sum of elements using sum function is 1.6 milliseconds.
Whereas, it took 8 milliseconds to calculate the same sum using for loop.
So, it is advisable to use inbuilt functions of R.
Let us learn how to calculate sum of each row and sum of each column.
In the Source window, type rowSums matrixA in parentheses.
Next, type colSums matrixA in parentheses.
Save the script and run these two lines to see the corresponding sums on the Console.
I am resizing the Console window to see the output properly.
Now let us learn how to add rows and columns to an existing data frame.
Click on the captaincy data frame.
Let us add a new row to the captaincy data frame.
Click on the script myMatrix.R
In the Source window, type the following command. Press Enter .
Press Enter after the comma for better visibility.
I am resizing the Source window
We have used rbind() function with the following arguments
name of the data frame to which we want to add the new row. Here, it is captaincy.
the row to be added as an argument to data.frame().
To data.frame() function, we provide the values according to the columns of the actual data frame.
names = “Kohli”
Y = 2016
played = 30
won = 20
lost = 9
victory = 20/30
In the Source window, type View within parentheses captaincy
Save the script and run the last two lines of code.
One new row with the details of Kohli is added in the captaincy data frame.
Now let us create a new column named defeat in the captaincy data frame.
Click on the script myMatrix.R.
In the Source window, type the following command.
Press Enter. Now we add defeat as a new column in captaincy data frame.
In the Source window, type the following command Press Enter.
We have used cbind() function with following two arguments:
name of the data frame to which we want to add the new column.
Here, it is captaincy. name of the column to be added. Here, it is defeat.
Now type View, captaincy in parentheses
Save the script and run the block of code starting from the declaration of defeat to the end.
defeat column is added in the captaincy data frame.
Let us summarize what we have learnt.
In this tutorial, we have learned how to:
Perform operation on matrices
Add rows or columns to a data frame
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.
Add another vector c(17,18,19,20) as a column to the previous matrix.
For solutions, 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.