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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 3: Line 3:
 
'''Author''': Shaik Sameer (IIIT Vadodara) and Sudhakar Kumar (IIT Bombay)
 
'''Author''': Shaik Sameer (IIIT Vadodara) and Sudhakar Kumar (IIT Bombay)
  
'''Keywords''': R, RStudio, matrices, data frames, video tutorial.
+
'''Keywords''': R, RStudio, matrices, data frames, transpose, determinant, video tutorial.
 
+
  
 
{| border =1
 
{| border =1
|'''Visual Cue'''
+
|'''Visual Cue’’’
|'''Narration'''
+
|'''Narration’’’
 
|-
 
|-
 
|| Show slide  
 
|| Show slide  
  
 
Opening slide
 
Opening slide
||Welcome to the spoken tutorial on Creating Matrices using Data Frames.'''
+
|| Welcome to the spoken tutorial on '''Creating Matrices using Data Frames.'''
|-
+
|-  
 
|| Show slide  
 
|| Show slide  
  
Line 24: Line 23:
  
  
|-
+
|-  
 
|| Show slide  
 
|| Show slide  
  
Line 30: Line 29:
  
 
spoken-tutorial.org
 
spoken-tutorial.org
|| To understand this tutorial, you should know* '''Data frames '''and '''Vectors''' in '''R'''
+
|| To understand this tutorial, you should know
 +
* '''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.
|-
+
|-  
 
|| Show slide  
 
|| Show slide  
  
 
System Specifications
 
System Specifications
  
 
+
|| This tutorial is recorded on
 
+
* '''Ubuntu Linux '''OS version''' 16.04'''
|| This tutorial is recorded on* '''Ubuntu Linux '''OS version''' 16.04'''
+
 
* '''R '''version''' 3.4.4'''
 
* '''R '''version''' 3.4.4'''
 
* '''RStudio''' version''' 1.1.456'''
 
* '''RStudio''' version''' 1.1.456'''
 
 
  
 
Install '''R''' version '''3.2.0''' or higher.  
 
Install '''R''' version '''3.2.0''' or higher.  
|-
+
|-  
 
|| Show slide  
 
|| Show slide  
  
Line 60: Line 55:
  
 
Please download these files from the '''Code files''' link of this tutorial.
 
Please download these files from the '''Code files''' link of this tutorial.
|-
+
|-  
 
|| [Computer screen]
 
|| [Computer screen]
  
 
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.'''
Line 82: Line 74:
  
 
'''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.  
Line 88: Line 80:
  
 
This prevents errors such as use of older data.  
 
This prevents errors such as use of older data.  
|-
+
|-  
 
|| Highlight '''Source''' button
 
|| Highlight '''Source''' button
  
Line 97: Line 89:
  
 
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 104: Line 96:
 
* 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'''.  
|-
+
|-  
 
||  
 
||  
 
|| Let us get back to '''RStudio'''.  
 
|| Let us get back to '''RStudio'''.  
|-
+
|-  
 
|| [RStudio]
 
|| [RStudio]
  
Line 122: Line 112:
  
 
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
 +
|| Click on the '''script myMatrix.R'''
 +
|-  
 
|| [RStudio]
 
|| [RStudio]
 
 
 
'''subdata <- captaincy[1:3, c("played", "won", "lost")]'''
 
'''subdata <- captaincy[1:3, c("played", "won", "lost")]'''
  
 +
|| In the '''Source '''window, type the following command Press '''Enter'''.
  
 +
I am resizing the '''Source''' window.
  
|| In '''Source '''window, type the following command and press '''Enter'''.  
+
Press '''Enter''' to go to the next line.  
  
 
Press '''Enter''' at the end of every command.  
 
Press '''Enter''' at the end of every command.  
|-
+
|-  
 
|| [RStudio]
 
|| [RStudio]
  
Line 142: Line 135:
  
 
'''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.
 
|| Save the '''script''' and execute the current line.  
 
|| Save the '''script''' and execute the current line.  
|-
+
|-  
 
|| [RStudio]
 
|| [RStudio]
  
Line 156: Line 149:
  
 
Click on the arrow before '''subData''' to expand its contents.  
 
Click on the arrow before '''subData''' to expand its contents.  
|-
+
|-  
 
||  
 
||  
 
|| Now, we will convert this '''subset''' into a '''matrix'''.
 
|| Now, we will convert this '''subset''' into a '''matrix'''.
|-
+
|-  
 
|| [RStudio]
 
|| [RStudio]
  
Line 168: Line 161:
  
 
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) '''
 
 
Press '''Ctrl + S''' >> Highlight two lines >> Press '''Ctrl+Enter''' keys.
 
 
|| 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.
Save the '''script''' and run these two lines.  
+
|| Save the '''script''' and run these two lines.  
|-
+
|-  
 
|| Highlight the output in the '''Console''' window
 
|| Highlight the output in the '''Console''' window
 
|| The content of '''matrixA''' is shown on the '''Console''' window.
 
|| The content of '''matrixA''' is shown on the '''Console''' window.
 
+
|-  
 
+
 
+
|-
+
 
|| cursor on the interface.
 
|| cursor on the interface.
 
|| 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]
  
 
'''values <- c(1,0,0,0,1,0,0,0,1)'''
 
'''values <- c(1,0,0,0,1,0,0,0,1)'''
 
|| In the '''Source''' window, type the following command and press '''Enter'''.  
 
|| In the '''Source''' window, type the following command and press '''Enter'''.  
|-
+
|-  
 
|| [RStudio]
 
|| [RStudio]
  
Line 205: Line 192:
 
|| 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 and press '''Enter'''.  
+
In the '''Source '''window, type the following command. Press '''Enter'''.
|-
+
 
 +
Press '''Enter''' to go to the next line.  
 +
|-  
 
|| [RStudio]
 
|| [RStudio]
  
Line 214: Line 203:
  
 
Highlight '''byrow''' in the '''Source''' window  
 
Highlight '''byrow''' in the '''Source''' window  
|| Here, I have used '''matrix''' function with following arguments:* data of '''values''' vector
+
|| Here, I have used '''matrix''' function with following arguments:
* number of rows as '''n row '''equal to''' 3 '''
+
* data of '''values''' vector
 +
* 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.  
  
 
+
|-  
|-
+
 
|| [RStudio]
 
|| [RStudio]
  
 
'''print(matrixB)'''
 
'''print(matrixB)'''
|| Now, type '''print '''and''' matrixB '''in parentheses 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.
 
|| Now let us perform some arithmetic operations on '''matrices'''.
 
|| Now let us perform some arithmetic operations on '''matrices'''.
|-
+
|-
 +
|| Highlight the boundary of '''Source''' window
 +
|| I will resize the '''Source''' window.
 +
|-  
 
|| [RStudio]
 
|| [RStudio]
  
Line 242: Line 234:
 
|| We will add and subtract '''matrices''' '''A''' and '''B'''.
 
|| We will add and subtract '''matrices''' '''A''' and '''B'''.
  
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.
  
 
Highlight the output on the '''Console''' window
 
Highlight the output on the '''Console''' window
 
|| Save the '''script''' and run these two lines.  
 
|| 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.  
 
The resulting matrices are shown on the '''Console''' window.  
|-
+
|-  
 
|| [RStudio]
 
|| [RStudio]
  
Line 258: Line 252:
 
|| Let us perform multiplication of individual elements of two '''matrices'''.
 
|| Let us perform multiplication of individual elements of two '''matrices'''.
  
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 these two lines.  
+
|| Save the '''script''' and run the current lines.  
  
The resulting matrices are 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
 
|| Now, we will perform a true '''matrix''' multiplication with these '''matrices'''.  
 
|| Now, we will perform a true '''matrix''' multiplication with these '''matrices'''.  
Line 272: Line 266:
 
1st row of '''matrixA''' is multiplied by 1st column of '''matrixB'''.  
 
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'''.  
+
Hence number of columns of '''matrixA''' should be equal to the number of rows of '''matrixB'''.  
|-
+
|-  
 
|| '''matrixA %*% matrixB '''
 
|| '''matrixA %*% matrixB '''
|| In the '''Source''' window,
+
|| In the '''Source''' window, type '''matrixA '''space''' percentage '''sign''' asterisk percentage '''sign''' '''space''' matrixB'''.  
 
+
|-  
type '''matrixA '''space''' percentage '''sign''' asterisk percentage '''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 these two lines.  
+
|| Save the '''script''' and run the current line.  
  
The resulting matrices are shown on the '''Console''' window.  
+
The resulting matrix is shown on the '''Console''' window.  
|-
+
|-  
 
|| Cursor on the interface.
 
|| Cursor on the interface.
 
|| Now let us calculate the '''transpose''' and '''determinant''' of '''matrixA'''.
 
|| Now let us calculate the '''transpose''' and '''determinant''' of '''matrixA'''.
|-
+
|-  
 
|| [RStudio]'''t(matrixA)'''
 
|| [RStudio]'''t(matrixA)'''
  
 
'''det(matrixA)'''
 
'''det(matrixA)'''
|| In the '''Source''' window, type''' t '''and '''matrixA '''in parentheses. Press '''Enter'''.  
+
|| In the '''Source''' window, type''' t '''and '''matrixA '''in parentheses.  
  
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 301: Line 293:
 
|| Save the '''script''' and run these two lines.  
 
|| Save the '''script''' and run these two lines.  
  
The corresponding output is shown on the '''Console'''
+
The corresponding output is shown on the '''Console '''window.
|-
+
|-  
 
||  
 
||  
 
|| Let us summarize what we have learnt.
 
|| Let us summarize what we have learnt.
|-
+
|-  
 
|| Show slide  
 
|| Show slide  
  
Line 319: Line 311:
  
  
|-
+
|-  
 
|| Show slide  
 
|| Show slide  
  
 
Assignment
 
Assignment
  
 
+
|| We now suggest an assignment.
 
+
* Consider 2 vectors c(9,10,11,12) and c(13,14,15,16).  
|| 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.  
 
* Create a 4 by 2 matrix from these two vectors.  
 
+
For solution, please refer to the '''Additional materials'' section on this website.  
 
+
|-  
 
+
<span style="background-color:#ffffff;">For solution, please refer to the </span><span style="background-color:#ffffff;">'''Additional materials'''</span><span style="background-color:#ffffff;"> section on this website. </span>
+
|-
+
 
|| Show slide
 
|| Show slide
  
Line 339: Line 327:
  
 
Please download and watch it.
 
Please download and watch it.
|-
+
|-  
 
|| Show slide
 
|| Show slide
  
Line 346: Line 334:
  
 
Please contact us.
 
Please contact us.
|-
+
|-  
 
|| Show Slide
 
|| Show Slide
  
 
Forum to answer questions
 
Forum to answer questions
 
|| Please post your timed queries in this forum.
 
|| Please post your timed queries in this forum.
|-
+
|-  
 
|| Show Slide
 
|| Show Slide
  
 
Forum to answer questions
 
Forum to answer questions
 
|| Please post your general queries in this forum.
 
|| Please post your general queries in this forum.
|-
+
|-  
 
|| Show Slide
 
|| Show Slide
  
 
Textbook Companion
 
Textbook Companion
||The '''FOSSEE '''team coordinates the '''TBC ''' project.
+
|| The '''FOSSEE ''' team coordinates the '''TBC '''project.
  
 
For more details, please visit these sites.
 
For more details, please visit these sites.
|-
+
|-  
 
|| Show Slide
 
|| Show Slide
  
 
Acknowledgement
 
Acknowledgement
 
|| The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
 
|| The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
|-
+
|-  
 
|| Show Slide
 
|| Show Slide
  

Revision as of 14:16, 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, 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 as dot 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