Difference between revisions of "R/C2/Creating-Matrices-using-Data-Frames/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(5 intermediate revisions by 3 users not shown)
Line 28: Line 28:
  
 
spoken-tutorial.org
 
spoken-tutorial.org
|| To understand this tutorial, you should know
+
|| To understand this tutorial, you should know,
* '''Data frames '''and '''Vectors''' in '''R'''
+
* '''Data frames''' and '''Vectors''' in '''R'''
* Basics''' '''of''' Matrices'''
+
* '''Basics''' of '''Matrices'''
 
* '''R script''' in '''RStudio'''
 
* '''R script''' in '''RStudio'''
* How to set '''working directory''' in''' RStudio'''
+
* How to set '''working directory''' in '''RStudio'''
  
 
If not, please locate the relevant tutorials on '''R''' on this website.
 
If not, please locate the relevant tutorials on '''R''' on this website.
Line 50: Line 50:
  
 
Download Files  
 
Download Files  
|| For this tutorial, we will use the '''data frame CaptaincyData.csv '''and a '''script '''file '''myMatrix.R'''.
+
||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.
 
Please download these files from the '''Code files''' link of this tutorial.
 
|-  
 
|-  
Line 57: Line 58:
 
Highlight '''CaptaincyData.csv '''and '''myMatrix.R''' in the folder '''Matrices '''
 
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'''.  
 
|| 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.'''  
 
I have also set '''Matrices''' folder as my '''Working Directory.'''  
 
|-  
 
|-  
 
||  
 
||  
|| Let us switch to '''RStudio'''.
+
|| Let us switch to '''RStudio'''.
 
|-  
 
|-  
 
|| Click '''myMatrix.R''' in '''RStudio'''
 
|| Click '''myMatrix.R''' in '''RStudio'''
  
 
Point to''' myMatrix.R''' in '''Rstudio.'''
 
Point to''' myMatrix.R''' in '''Rstudio.'''
|| Open the '''script myMatrix.R''' in '''RStudio.'''
+
|| Open the '''script myMatrix.R''' in '''RStudio'''.
  
 
For this, click on the '''script myMatrix.R'''  
 
For this, click on the '''script myMatrix.R'''  
  
'''Script myMatrix.R '''opens in''' RStudio.'''
+
'''Script myMatrix.R''' opens in '''RStudio'''.
|-  
+
|-
|| Highlight '''rm '''in '''Source '''window
+
|| Highlight '''rm''' in '''Source '''window
|| Here, I have used '''rm '''function to remove all existing variables from memory.  
+
|| Here, I have used '''rm function''' to remove all existing variables from memory.  
  
 
This prevents errors such as use of older data.  
 
This prevents errors such as use of older data.  
 
|-  
 
|-  
 
|| Highlight '''Source''' button
 
|| Highlight '''Source''' button
 
  
 
Highlight '''captaincy''' in the '''Source''' window  
 
Highlight '''captaincy''' in the '''Source''' window  
 
|| Run this '''script''' by clicking on the '''Source '''button.  
 
|| Run this '''script''' by clicking on the '''Source '''button.  
  
The captaincy '''data frame''' is displayed in the '''Source '''window.  
+
The captaincy '''data frame''' is displayed in the '''Source''' window.  
 
|-  
 
|-  
 
|| Show slide  
 
|| Show slide  
Line 88: Line 89:
 
Data in Matrix Format
 
Data in Matrix Format
 
|| We need our data in a '''matrix''' form to:
 
|| We need our data in a '''matrix''' form to:
* Calculate the inverse of a''' matrix'''
+
* Calculate the inverse of a '''matrix'''
* Perform matrix operations like addition, subtraction, multiplication, etc.
+
* Perform '''matrix''' operations like addition, subtraction, multiplication, etc.
  
Hence, we need to learn how to convert a''' data frame''' into a '''matrix'''.  
+
Hence, we need to learn how to convert a '''data frame''' into a '''matrix'''.
 
|-  
 
|-  
 
||  
 
||  
Line 97: Line 98:
 
|-  
 
|-  
 
|| [RStudio]
 
|| [RStudio]
 
  
 
Highlight the three rows of '''captaincy'''
 
Highlight the three rows of '''captaincy'''
Line 103: Line 103:
 
|| We will convert the values in first three rows of '''played''', '''won''' and '''lost''' columns into a '''matrix'''.  
 
|| 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.
+
We will create a '''subset''' for extracting these values.
 
|-  
 
|-  
 
|| Highlight '''myMatrix.R '''in the '''Source''' window  
 
|| Highlight '''myMatrix.R '''in the '''Source''' window  
Line 121: Line 121:
 
|| [RStudio]
 
|| [RStudio]
  
Highlight''' 1:3 '''in the '''Source '''window  
+
Highlight '''1:3''' in the '''Source '''window  
  
 
Highlight '''c''' in the '''Source '''window  
 
Highlight '''c''' in the '''Source '''window  
 
|| '''1 colon 3''' indicates that the first three rows are to be extracted.  
 
|| '''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.  
+
'''c''' is the '''concatenate vector command''' with names of each column within double quotes.  
 
|-  
 
|-  
 
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
 
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
Line 133: Line 133:
 
|| [RStudio]
 
|| [RStudio]
  
 
+
Highlight '''subData '''in the '''Environment''' window.
Highlight '''subData '''in the '''Environment '''window.
+
  
 
Click on the arrow before '''subData'''.
 
Click on the arrow before '''subData'''.
Line 147: Line 146:
  
 
'''matrixA <- as.matrix(subdata)'''
 
'''matrixA <- as.matrix(subdata)'''
|| In the '''Source '''window, type '''matrixA '''then press '''Alt '''and '''-'''('''hyphen''')''' '''keys simultaneously.
+
|| In the '''Source''' window, type '''matrixA''' then press '''Alt''' and '''hyphen'''(-)keys simultaneously.
  
Now type '''as '''dot '''matrix''', within parentheses type '''subdata '''and press '''Enter'''.  
+
Now type '''as'''dot '''matrix''', within parentheses type '''subdata''' and press '''Enter'''.  
 
|-  
 
|-  
|| Highlight '''as.matrix''' in the '''Source '''window
+
|| Highlight '''as.matrix''' in the '''Source''' window
|| '''as.matrix''' function is used to convert the '''subdata '''into a '''matrix'''.  
+
|| '''as.matrix function''' is used to convert the '''subdata '''into a '''matrix'''.  
 
|-  
 
|-  
 
|| [RStudio]
 
|| [RStudio]
  
 
'''print(matrixA) '''
 
'''print(matrixA) '''
|| In '''Source''' window, type '''print, '''within parentheses''' matrixA'''.  
+
|| In '''Source''' window, type '''print''', within '''parentheses''' '''matrixA'''.  
 
|-  
 
|-  
 
|| Press '''Ctrl + S''' >> Highlight two lines >> Press '''Ctrl+Enter''' keys.
 
|| Press '''Ctrl + S''' >> Highlight two lines >> Press '''Ctrl+Enter''' keys.
Line 168: Line 167:
 
|| Now let us create a 3 by 3 '''identity matrix'''.
 
|| 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.
+
Create a '''vector''' named '''values''' containing the row-wise values of a 3 by 3 identity matrix.
 
|-  
 
|-  
 
|| [RStudio]
 
|| [RStudio]
Line 178: Line 177:
  
 
'''matrixB <- matrix(values, nrow = 3, ncol = 3, byrow = TRUE)'''
 
'''matrixB <- matrix(values, nrow = 3, ncol = 3, byrow = TRUE)'''
|| Now we will create a variable '''matrixB''' to store the '''matrix''' created using vector '''values'''.
+
|| 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'''.
+
In the '''Source''' window, type the following command. Press '''Enter'''.
  
 
Press '''Enter''' to go to the next line.  
 
Press '''Enter''' to go to the next line.  
Line 191: Line 190:
  
 
Highlight '''byrow''' in the '''Source''' window  
 
Highlight '''byrow''' in the '''Source''' window  
|| Here, I have used '''matrix''' function with following arguments:
+
|| Here, I have used '''matrix function''' with following '''arguments''':
 
* data of '''values''' vector
 
* data of '''values''' vector
* number of rows as '''nrow '''equal to''' 3 '''
+
* number of rows as '''nrow''' equal to '''3'''
* number of columns as '''ncol '''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.  
+
* '''byrow '''equal to''' TRUE''' indicates that, data provided has to be arranged row-wise in the '''matrix'''.  
  
 
|-  
 
|-  
Line 201: Line 200:
  
 
'''print(matrixB)'''
 
'''print(matrixB)'''
|| Now, type '''print '''within parentheses type''' matrixB '''to show the contents of the matrix.  
+
|| Now, type '''print''' within parentheses type '''matrixB''' to show the contents of the '''matrix'''.  
 
|-  
 
|-  
 
|| Press''' Ctrl + S''' keys >> Highlight three lines >> Press '''Ctrl + Enter''' keys  
 
|| Press''' Ctrl + S''' keys >> Highlight three lines >> Press '''Ctrl + Enter''' keys  
|| Save the '''script''' and run the last three lines''' '''of code.
+
|| Save the '''script''' and run the last three lines of code.
 
|-  
 
|-  
 
|| Highlight the output in the '''Console''' window
 
|| Highlight the output in the '''Console''' window
|| The '''identity''' '''matrix''' is shown on the '''Console''' window.  
+
|| The '''identity matrix''' is shown on the '''Console''' window.  
 
|-  
 
|-  
 
|| Cursor on the interface.
 
|| Cursor on the interface.
Line 220: Line 219:
  
 
'''matrixA - matrixB '''
 
'''matrixA - matrixB '''
|| We will add and subtract '''matrices''' '''A''' and '''B'''.
+
|| We will add and subtract '''matrices A''' and '''B'''.
  
In the '''Source''' window, type '''matrixA '''space''' plus '''sign''' '''space''' matrixB '''
+
In the '''Source''' window, type '''matrixA''' space '''plus''' sign space '''matrixB'''.
  
Next, type '''matrixA '''space''' minus '''sign''' '''space''' matrixB '''
+
Next, type '''matrixA''' space '''minus''' sign space ''' ''matrixB'''
 
|-  
 
|-  
 
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
 
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
Line 233: Line 232:
 
I will resize the '''Console''' window to see the output properly.  
 
I will resize the '''Console''' window to see the output properly.  
  
The resulting matrices are shown on the '''Console''' window.  
+
The resulting '''matrices''' are shown on the '''Console''' window.  
 
|-  
 
|-  
 
|| [RStudio]
 
|| [RStudio]
  
 
'''matrixA * matrixB '''
 
'''matrixA * matrixB '''
|| Let us perform multiplication of individual elements of two '''matrices'''.
+
|| Let us perform multiplication of individual '''elements''' of two '''matrices'''.
  
In the Source window, type '''matrixA '''space''' asterisk '''sign''' '''space''' matrixB '''
+
In the '''Source''' window, type '''matrixA''' space '''asterisk''' sign space '''matrixB'''
 
|-  
 
|-  
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
+
|| Press '''Ctrl + S''' >> Press '''Ctrl + Enter''' keys.
  
Highlight the output on the '''Console''' window
+
Highlight the output on the '''Console''' window.
 
|| Save the '''script''' and run the current lines.  
 
|| Save the '''script''' and run the current lines.  
  
The resulting matrix is shown on the '''Console''' window.  
+
The resulting '''matrix''' is shown on the '''Console''' window.  
 
|-  
 
|-  
 
|| Highlight '''matrixA''' and '''matrixB''' in '''Environment''' window
 
|| Highlight '''matrixA''' and '''matrixB''' in '''Environment''' window
Line 257: Line 256:
 
|-  
 
|-  
 
|| '''matrixA %*% matrixB '''
 
|| '''matrixA %*% matrixB '''
|| In the '''Source''' window, type '''matrixA '''space''' percentage '''sign''' asterisk percentage '''sign''' '''space''' matrixB'''.  
+
|| In the '''Source''' window, type '''matrixA''' space '''percentage''' sign '''asterisk percentage''' sign space '''matrixB'''.  
 
|-  
 
|-  
 
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
 
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
Line 264: Line 263:
 
|| Save the '''script''' and run the current line.  
 
|| Save the '''script''' and run the current line.  
  
The resulting matrix is shown on the '''Console''' window.  
+
The resulting '''matrix''' is shown on the '''Console''' window.  
 
|-  
 
|-  
 
|| Cursor on the interface.
 
|| Cursor on the interface.
Line 272: Line 271:
  
 
'''det(matrixA)'''
 
'''det(matrixA)'''
|| In the '''Source''' window, type''' t '''and '''matrixA '''in parentheses.  
+
|| In the '''Source''' window, type '''t''' and '''matrixA''' in parentheses.  
  
Now, type '''det '''and''' matrixA '''in parentheses.  
+
Now, type '''det''' and '''matrixA''' in parentheses.  
 
|-  
 
|-  
 
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
 
|| Press '''Ctrl + S''' >> Press '''Ctrl+Enter''' keys.
Line 281: Line 280:
 
|| Save the '''script''' and run these two lines.  
 
|| Save the '''script''' and run these two lines.  
  
The corresponding output is shown on the '''Console '''window.  
+
The corresponding output is shown on the '''Console''' window.  
 
|-  
 
|-  
 
||  
 
||  
Line 289: Line 288:
  
 
Summary
 
Summary
|| In this tutorial, we have learnt how to: * Convert a '''data frame''' into a '''matrix'''
+
|| In this tutorial, we have learnt how to:  
 +
* Convert a '''data frame''' into a '''matrix'''
 
* Create a '''matrix''' in '''R'''
 
* Create a '''matrix''' in '''R'''
 
* Perform basic operations on '''matrices'''
 
* Perform basic operations on '''matrices'''
Line 299: Line 299:
  
 
|| We now suggest an assignment.
 
|| We now suggest an assignment.
* Consider 2 vectors c(9,10,11,12) and c(13,14,15,16).  
+
* Consider 2 '''vectors''' c(9,10,11,12) and c(13,14,15,16).  
* Create a 4 by 2 matrix from these two vectors.  
+
* Create a 4 by 2 '''matrix''' from these two '''vectors'''.  
For solution, please refer to the '''Additional materials'' section on this website.  
+
 
 +
For solution, please refer to the '''Additional materials''' section on this website.  
 
|-  
 
|-  
 
|| Show slide
 
|| Show slide

Latest revision as of 21:55, 5 March 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, transpose, determinant, 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:
  • Convert a data frame into a matrix
  • Create a matrix in R
  • Perform basic operations on matrices
Show slide

Pre-requisites

spoken-tutorial.org

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.

Show slide

System Specifications

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.

Show slide

Download Files

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.

[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.

I have also set Matrices folder as my Working Directory.

Let us switch to RStudio.
Click myMatrix.R in RStudio

Point to myMatrix.R in Rstudio.

Open the script myMatrix.R in RStudio.

For this, click on the script myMatrix.R

Script myMatrix.R opens in RStudio.

Highlight rm in Source window Here, I have used rm function to remove all existing variables from memory.

This prevents errors such as use of older data.

Highlight Source button

Highlight captaincy in the Source window

Run this script by clicking on the Source button.

The captaincy data frame is displayed in the Source window.

Show slide

Data in Matrix Format

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.
[RStudio]

Highlight the three rows of captaincy

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.

Highlight myMatrix.R in the Source window Click on the script myMatrix.R
[RStudio]

subdata <- captaincy[1:3, c("played", "won", "lost")]

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.

[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]

Highlight subData in the Environment window.

Click on the arrow before subData.

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]

matrixA <- as.matrix(subdata)

In the Source window, type matrixA then press Alt and hyphen(-)keys simultaneously.

Now type asdot matrix, within parentheses type subdata and press Enter.

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.
Press Ctrl + S >> Highlight two lines >> Press Ctrl+Enter keys. Save the script and run these two lines.
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]

matrixB <- matrix(values, nrow = 3, ncol = 3, byrow = TRUE)

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.

[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
  • 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.
[RStudio]

print(matrixB)

Now, type print within parentheses type matrixB 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.
Highlight the boundary of Source window I will resize the Source window.
[RStudio]

matrixA + matrixB

matrixA - matrixB

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

Press Ctrl + S >> Press Ctrl+Enter keys.

Highlight the output on the Console window

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.

[RStudio]

matrixA * matrixB

Let us perform multiplication of individual elements of two matrices.

In the Source window, 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 the current lines.

The resulting matrix is 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 the 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 the current line.

The resulting matrix is 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.

Now, 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 window.

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
  • Create a matrix in R
  • Perform basic operations on matrices
Show slide

Assignment

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.

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).

This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Madhurig, Nancyvarkey, Sudhakarst