R/C2/Creating-Matrices-using-Data-Frames/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search


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

Contributors and Content Editors

Sakinashaikh