Difference between revisions of "R/C2/Creating-Matrices-using-Data-Frames/English"
Sudhakarst (Talk | contribs) (Created page with "'''Title of script''': Creating Matrices using Data Frames '''Author''': Shaik Sameer (IIIT Vadodara) and Sudhakar Kumar (IIT Bombay) '''Keywords''': R, RStudio, matrices,...") |
Sudhakarst (Talk | contribs) |
||
Line 13: | Line 13: | ||
Opening slide | Opening slide | ||
− | ||Welcome to the spoken tutorial | + | ||Welcome to the spoken tutorial on Creating Matrices using Data Frames.''' |
|- | |- | ||
|| Show slide | || Show slide |
Revision as of 13:10, 28 February 2019
Title of script: Creating Matrices using Data Frames
Author: Shaik Sameer (IIIT Vadodara) and Sudhakar Kumar (IIT Bombay)
Keywords: R, RStudio, matrices, data frames, video tutorial.
Visual Cue | Narration |
Show slide
Opening slide |
Welcome to the spoken tutorial on Creating Matrices using Data Frames. |
Show slide
Learning Objectives |
In this tutorial, we will learn how to:
|
Show slide
Pre-requisites spoken-tutorial.org |
To understand this tutorial, you should know* Data frames and Vectors in R
If not, please locate the relevant tutorials on R on this website. |
Show slide
System Specifications
|
This tutorial is recorded on* Ubuntu Linux OS version 16.04
Install R version 3.2.0 or higher. |
Show slide
Download Files |
For this tutorial, we will use the data frame CaptaincyData.csv and a script file myMatrix.R.
|
[Computer screen]
Highlight CaptaincyData.csv and myMatrix.R in the folder Matrices |
I have downloaded and moved these files to Matrices folder in myProject folder on the Desktop.
|
Let us switch to RStudio. | |
Click myMatrix.R in RStudio
|
Open the script myMatrix.R in RStudio.
For this, click on the script myMatrix.R
|
Highlight rm in Source window | Here, I have used rm function to remove all existing variables from memory.
|
Highlight Source button
|
Run this script by clicking on the Source button.
|
Show slide
Data in Matrix Format |
We need our data in a matrix form to:
Hence, we need to learn how to convert a data frame into a matrix. |
Let us get back to RStudio. | |
[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. |
[RStudio]
|
In Source window, type the following command and press Enter.
Press Enter at the end of every command. |
[RStudio]
Highlight 1:3 in the Source window Highlight c in the Source window |
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. |
Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and execute the current line. |
[RStudio]
|
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. | |
[RStudio]
|
In the Source window, type matrixA then press Alt and -(hyphen) keys simultaneously.
|
Highlight as.matrix in the Source window | as.matrix function is used to convert the subdata into a matrix. |
[RStudio]
print(matrixA)
|
In Source window, type print, within parentheses matrixA.
|
Highlight the output in the Console window | The content of matrixA is shown on the Console window.
|
cursor on the interface. | 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. |
[RStudio]
values <- c(1,0,0,0,1,0,0,0,1) |
In the Source window, type the following command and press Enter. |
[RStudio]
|
Now we will create a variable matrixB to store the matrix created using vector values.
In the Source window, type the following command and press Enter. |
[RStudio]
Highlight values in the Source window Highlight nrow and ncol in the Source window Highlight byrow in the Source window |
Here, I have used matrix function with following arguments:* data of values vector
|
[RStudio]
print(matrixB) |
Now, type print and matrixB in parentheses to show the contents of the matrix. |
Press Ctrl + S keys >> Highlight three lines >> Press Ctrl + Enter keys | Save the script and run the last three lines of code. |
Highlight the output in the Console window | The identity matrix is shown on the Console window. |
Cursor on the interface. | Now let us perform some arithmetic operations on matrices. |
[RStudio]
matrixA + matrixB matrixA - matrixB |
We will add and subtract matrices A and B.
Type matrixA space plus sign space matrixB Next, type matrixA space minus sign space matrixB |
Press Ctrl + S >> Press Ctrl+Enter keys.
Highlight the output on the Console window |
Save the script and run these two lines.
The resulting matrices are shown on the Console window. |
[RStudio]
matrixA * matrixB |
Let us perform multiplication of individual elements of two matrices.
Type matrixA space asterisk sign space matrixB |
Press Ctrl + S >> Press Ctrl+Enter keys.
Highlight the output on the Console window |
Save the script and run these two lines.
The resulting matrices are shown on the Console window. |
Highlight matrixA and matrixB in Environment 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 number of rows of matrixB. |
matrixA %*% matrixB | In the Source window,
type matrixA space percentage sign asterisk percentage sign space matrixB. |
Press Ctrl + S >> Press Ctrl+Enter keys.
Highlight the output on the Console window. |
Save the script and run these two lines.
The resulting matrices are shown on the Console window. |
Cursor on the interface. | Now let us calculate the transpose and determinant of matrixA. |
[RStudio]t(matrixA)
det(matrixA) |
In the Source window, type t and matrixA in parentheses. Press Enter.
Type det and matrixA in parentheses. |
Press Ctrl + S >> Press Ctrl+Enter keys.
Highlight the output on the Console window. |
Save the script and run these two lines.
The corresponding output is shown on the Console |
Let us summarize what we have learnt. | |
Show slide
Summary
|
In this tutorial, we have learnt how to: * Convert a data frame into a matrix
|
Show slide
Assignment
|
We now suggest an assignment.* Consider 2 vectors c(9,10,11,12) and c(13,14,15,16).
For solution, please refer to the Additional materials section on this website. |
Show slide
About the Spoken Tutorial Project |
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it. |
Show slide
Spoken Tutorial Workshops |
We conduct workshops using Spoken Tutorials and give certificates.
Please contact us. |
Show Slide
Forum to answer questions |
Please post your timed queries in this forum. |
Show Slide
Forum to answer questions |
Please post your general queries in this forum. |
Show Slide
Textbook Companion |
The FOSSEE team coordinates the TBC project.
For more details, please visit these sites. |
Show Slide
Acknowledgement |
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India |
Show Slide
Thank You |
The script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018).
|