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

From Script | Spoken-Tutorial
Revision as of 19:13, 3 March 2019 by Sudhakarst (Talk | contribs)

Jump to: navigation, search

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