Difference between revisions of "R/C2/Operations-on-Matrices-and-Data-Frames/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| border =1 |'''Time''' |'''Narration''' |- || 00:01 || Welcome to the spoken tutorial on '''Operations on Matrices and Data Frames'''. |- || 00:08 || In this tutorial,...")
 
(No difference)

Latest revision as of 15:46, 22 May 2020


Time Narration
00:01 Welcome to the spoken tutorial on Operations on Matrices and Data Frames.
00:08 In this tutorial, we will learn how to:
00:13 Perform operations on matrices
00:16 Add rows or columns to a data frame
00:20 To understand this tutorial, you should know
00:24 Data frames and Matrices in R
00:27 R script in RStudio
00:32 How to set working directory in RStudio
00:37 If not, please locate the relevant tutorials on R on this website.
00:44 This tutorial is recorded on
00:47 Ubuntu Linux OS version 16.04
00:53 R version 3.4.4
00:58 RStudio version 1.1.456
01:05 Install R version 3.2.0 or higher.
01:10 For this tutorial, we will use the data frame CaptaincyData.csv and the script file myMatrix.R.
01:21 Please download these files from the Code files link of this tutorial.
01:28 I have downloaded and moved these files to MatrixOps folder' in myProject folder on the Desktop.
01:39 I have also set MatrixOps folder as my Working directory.
01:46 Let us switch to Rstudio.
01:51 Open the script myMatrix.R in RStudio.
01:56 For this, click on the script myMatrix.R
02:02 Script myMatrix.R opens in Rstudio.
02:08 Recall that, we had created a matrix named matrixA.
02:14 This matrix was extracted as a subset from the captaincy data frame.
02:21 We will use matrixA here also.
02:25 Run this script myMatrix.R to load the values in the Workspace.
02:32 captaincy data frame opens in the Source window.
02:37 I will resize the Source window.
02:41 Now let us learn how to find the inverse of matrixA.
02:48 Click on the script myMatrix.R
02:53 In the Source window, type solve, within parentheses matrixA.
03:00 Press Enter. Press Enter at the end of every command.
03:07 Save the script and run this line.
03:12 The inverse of matrixA in shown in the Console window.
03:18 For more information on calculating inverse of a matrix in R, please refer to the Additional materials section on this website.
03:29 Now let us calculate the sum of all the elements in matrixA.
03:36 First, we shall use nested for loops for calculating this sum.
03:43 Also, we shall estimate the time taken to calculate the sum in this way.
03:50 Let us add a comment - Calculating sum using for loop in the script.
03:57 In the Source window, type # hash space Calculating sum using for loop.
04:05 Press Enter. To calculate the time taken, we record the present time.
04:12 In the Source window, type start Time with capital T.
04:18 Then press Alt and -(hyphen) keys simultaneously.
04:23 Next, type Sys dot time followed by parentheses.
04:30 Sys.time() is used to find the absolute date-time value in the current time.
04:38 Let us initialise a variable totalSum.
04:43 In the Source window, type totalSum
04:47 then press Alt and -(hyphen) keys simultaneously. Then type zero.
04:55 This variable will store the sum of elements in matrixA.
05:01 Now, we will create two for loops to iterate through the rows and columns of the matrixA.
05:10 In the Source window, type for in parentheses i space in space 1 colon 3 space
05:21 Now type opening curly bracket. RStudio automatically adds a closing curly bracket.
05:30 Press Enter just after the opening curly bracket and type the following command.
05:39 Press Enter after the closing curly bracket of outer loop for(i in 1:3).
05:48 In the Source window, type print totalSum in parentheses.
05:54 Now, we record the current time again.
05:59 Type endTime then press Alt and -(hyphen) keys simultaneously. Type Sys dot time parentheses.
06:10 Press Enter. Type endTime space minus sign space startTime to know the total time taken.

And save the script.

06:23 I am resizing the Source window.
06:27 Run the block of code from the comment line Calculating sum using for loop to the end.
06:35 The totalSum is evaluated to be 237.
06:41 The time taken to calculate the sum of elements in matrixA using for loop is approximately 8 milliseconds.
06:51 However, it may vary from system to system.
06:56 Now, let us calculate the sum of all elements in matrixA using the sum function.
07:05 We shall estimate the time taken to calculate the sum in this way also.
07:12 In the script myMatrix.R, add a comment Calculating sum using inbuilt function.
07:21 In the Source window, type # hash space Calculating sum using inbuilt function.
07:30 Now, type the following commands.
07:35 Save the script and run the block of code from the comment line Calculating sum using inbuilt function to the end.
07:46 The time taken to calculate the sum of elements using sum function is 1.6 milliseconds.
07:56 Whereas, it took 8 milliseconds to calculate the same sum using for loop.
08:04 So, it is advisable to use inbuilt functions of R.
08:10 Let us learn how to calculate sum of each row and sum of each column.
08:18 In the Source window, type rowSums matrixA in parentheses.
08:26 Next, type colSums matrixA in parentheses.
08:32 Save the script and run these two lines to see the corresponding sums on the Console.
08:40 I am resizing the Console window to see the output properly.
08:47 Now let us learn how to add rows and columns to an existing data frame.
08:55 Click on the captaincy data frame.
08:59 Let us add a new row to the captaincy data frame.
09:04 Click on the script myMatrix.R
09:09 In the Source window, type the following command. Press Enter .
09:24 Press Enter after the comma for better visibility.
09:30 I am resizing the Source window
09:34 We have used rbind() function with the following arguments
09:40 name of the data frame to which we want to add the new row. Here, it is captaincy.
09:48 the row to be added as an argument to data.frame().
09:54 To data.frame() function, we provide the values according to the columns of the actual data frame.
10:03 names = “Kohli”
10:06 Y = 2016
10:10 played = 30
10:13 won = 20
10:17 lost = 9
10:20 victory = 20/30
10:24 In the Source window, type View within parentheses captaincy
10:30 Save the script and run the last two lines of code.
10:37 One new row with the details of Kohli is added in the captaincy data frame.
10:44 Now let us create a new column named defeat in the captaincy data frame.
10:52 Click on the script myMatrix.R.
10:57 In the Source window, type the following command.
11:03 Press Enter. Now we add defeat as a new column in captaincy data frame.
11:11 In the Source window, type the following command Press Enter.
11:17 We have used cbind() function with following two arguments:
11:23 name of the data frame to which we want to add the new column.
11:28 Here, it is captaincy. name of the column to be added. Here, it is defeat.
11:37 Now type View, captaincy in parentheses
11:42 Save the script and run the block of code starting from the declaration of defeat to the end.
11:52 defeat column is added in the captaincy data frame.
11:58 Let us summarize what we have learnt.
12:04 In this tutorial, we have learned how to:
12:07 Perform operation on matrices
12:11 Add rows or columns to a data frame
12:16 We now suggest an assignment.
12:20 Consider 2 vectors c(9,10,11,12) and c(13,14,15,16).
12:30 Create a 4 by 2 matrix from these two vectors.
12:35 Add another vector c(17,18,19,20) as a column to the previous matrix.
12:45 For solutions, please refer to the Additional materials section on this website.
12:52 The video at the following link summarises the Spoken Tutorial project.
12:58 Please download and watch it.
13:01 We conduct workshops using Spoken Tutorials and give certificates. Please contact us.
13:11 Please post your timed queries in this forum.
13:16 Please post your general queries in this forum.
13:21 The FOSSEE team coordinates the TBC project. For more details, please visit these sites.
13:30 The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
13:37 The script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018).
13:46 This is Sudhakar Kumar from IIT Bombay signing off.
13:51 Thanks for watching.

Contributors and Content Editors

Sakinashaikh