SlideShare a Scribd company logo
www.r-squared.in/git-hub
R2
Academy R Programming: Matrix
R2
AcademyCourse Material
Slide 2
All the material related to this course are available on our website
Scripts can be downloaded from GitHub
Videos can be viewed on our Youtube Channel
R2
AcademyObjectives
Slide 3
➢ Create matrices
➢ Matrix Operations
➢ Combine matrices
➢ Index/Subset matrix
➢ Dissolve matrix
R2
Academy
Slide 4
COLUMNS
1 2 3
R
O 4 5 6
W
S 7 8 9
10 11 12
A matrix is a rectangular array of data elements, arranged in rows and columns. Matrices in R are
homogeneous i.e. they can hold a single type of data. Matrix elements are indexed by specifying the
row and column index and the elements of a matrix can be filled by row or columns. In the first
section, we look at various methods of creating matrices in R.
R2
Academy
Slide 5
The easiest way to create a matrix in R is to use the function. Let us look at its
syntax:
data Data Elements of the matrix Integer/Numeric/Logical/Character
nrow Number of rows Integer
ncol Number of columns Integer
byrow Whether data should be filled by rows Logical
dimnames Names of rows and columns Character vector
R2
Academy
Slide 6
Now that we have understood the syntax of the function, let us create a simple
numeric matrix:
R2
Academy
Slide 7
In the previous example, we created a matrix of 3 rows where the data elements are filled by
columns. We need to specify either the number of or and R will automatically
calculate the other. The number of data elements should be equal to the product of number of
rows and columns, else R will return an warning message.
R2
Academy
Slide 8
We can follow some general rules to avoid the mistakes made in the
previous two examples:
● If the number of data elements are odd, both the number of rows
and columns must be odd and their product should equal the
number of data elements.
● If the number of data elements are even, either the number of
rows must be even or the number of columns must be even. In
certain cases, both of them must be even.
R2
Academy
Slide 9
Let us continue to explore the syntax of the function. Let us create two matrices, the data
elements of which are filled by rows in the first case, and columns in the second case.
R2
Academy
Slide 10
Either the number of rows or columns need to be specified and R will calculate the other one automatically. We create
two matrices below, the first one specified by rows and the second one by columns.
R2
Academy
Slide 11
If we want to specify the names of the rows and columns, we need to use a data structure called . Lists can
contain other data structures including other lists. They are heterogeneous i.e. they can contain different data types.
We will learn more about lists in the next unit. For now, let us learn to create a basic list. Lists in R can be created using
the function.
R2
Academy
Slide 12
Let us now create a list of row and column names and use it to name the rows and columns of a
matrix.
R2
Academy
Slide 13
Another method to create a matrix is to use the function. It is basically used to check or specify the dimensions
of a data structure. In case of matrices, it returns the number of rows and columns. Let us look at a few examples:
R2
Academy
Slide 14
In the below example, we use the function to change the dimension of the matrix.In the dim function, we need to
mention both the number of rows and columns using the function. We change the rows from 3 to 4 and the
columns from 4 to 3.
R2
Academy
Slide 15
In the below example, we use the function to change row from 2 to 6 and the columns from 6 to 2.
R2
Academy
Slide 16
Now that we have understood the function, let us use it to convert vectors to matrices. Below are a few examples:
R2
Academy
Slide 17
R2
Academy
Slide 18
The last method that we will explore in this section is the function. It is used to coerce a
data structure to the type . Since the only other data structure that we have covered so far is
the vector, we will coerce a vector to type We will deal with other data structures as and
when we learn about them.
R2
Academy
Slide 19
Regardless of the data type of vector, all of them are coerced to a of dimension n x 1 i.e. they
will all have only one column.
R2
Academy
Slide 20
In this section, we will cover the following:
● Matrix Addition
● Matrix Subtraction
● Matrix Division
● Transpose of a Matrix
● Matrix Multiplication
● Inverse of a Matrix
The four basic operations of addition, subtraction, multiplication and division can be done by element
wise or with a scalar value. We will be looking at both cases. In the case of multiplication, we need to
compute the transpose of the matrix before we can do element wise multiplication.
R2
Academy
Slide 21
R2
Academy
Slide 22
R2
Academy
Slide 23
R2
Academy
Slide 24
R2
Academy
Slide 25
We can use the function to compute the transpose of a matrix.
R2
Academy
Slide 26
We need to follow the basic rules of matrix multiplication i.e. the number of columns in the first matrix
should be equal to the number of rows in the second matrix. Let us look at an example:
R2
Academy
Slide 27
The inverse of a matrix can be computed using the function.
R2
Academy
Slide 28
In this section, we will focus on appending vector to matrices and combining matrices. There are two
functions that can be used for this purpose:
●
●
will combine/append the data by columns while will do the same by rows. When you
use to combine two matrices, the number of columns must match and in case of , the
number of rows must match.
R2
Academy
Slide 29
R2
Academy
Slide 30
R2
Academy
Slide 31
R2
Academy
Slide 32
R2
Academy
Slide 33
In this section, we will learn to index/subset elements of a matrix. The operator can be used to index
the elements as we did in case of vectors but since matrices are two dimensional, we need to specify
both the row number and the column number. Below are a few examples:
R2
Academy
Slide 34
R2
Academy
Slide 35
R2
Academy
Slide 36
In an earlier section, we learnt how to name the rows and columns of a matrix. Let us see how these
names can be used to subset matrices.
R2
Academy
Slide 37
When you are using names of columns or rows for subsetting data from matrices, ensure that they
are enclosed in single or double quotes.
R2
Academy
Slide 38
We can use logical expressions to subset elements of a matrix.
R2
Academy
Slide 39
So far, we have learnt how to coerce a vector to a matrix. In this final section, we will learn to
coerce a matrix to a vector. We can use the following functions:
●
●
R2
Academy
Slide 40
● Matrices are two dimensional arrays.
● They are homogeneous i.e. they can hold single type of data.
● The easiest way to create a matrix is by using the function.
● The function can be used to specify the dimensions of a matrix.
● They can be indexed using or names of rows/columns.
● Out of range index returns error.
● Negative index drops row/column from the matrix.
● Use function for transpose and function for inverse of a matrix.
● and can be used to append vectors and combine matrices.
● Logical expressions can be used to subset matrices.
R2
AcademyNext Steps...
Slide 41
In the next module:
✓ Construct Lists
✓ Describe Lists
✓ Index/Subset List Elements
✓ Convert Lists
R2
Academy
Slide 42
Visit Rsquared Academy
for tutorials on:
→ R Programming
→ Business Analytics
→ Data Visualization
→ Web Applications
→ Package Development
→ Git & GitHub

More Related Content

What's hot

3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
krishna singh
 
R data types
R data typesR data types
R data types
Teachers Mitraa
 
R factors
R   factorsR   factors
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With R
Edureka!
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
Malla Reddy University
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
Abhishek Singh
 
Python list
Python listPython list
Python list
Mohammed Sikander
 
An Introduction to Data Mining with R
An Introduction to Data Mining with RAn Introduction to Data Mining with R
An Introduction to Data Mining with R
Yanchang Zhao
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
FAO
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in r
Dr Nisha Arora
 
R programming slides
R  programming slidesR  programming slides
R programming slides
Pankaj Saini
 
R programming presentation
R programming presentationR programming presentation
R programming presentation
Akshat Sharma
 
R data types
R   data typesR   data types
R data types
Learnbay Datascience
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Melaku Bayih Demessie
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
ExternalEvents
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
Guy Lebanon
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
GirdharRatne
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
Nimrita Koul
 

What's hot (20)

3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
 
Reading Data into R
Reading Data into RReading Data into R
Reading Data into R
 
R data types
R data typesR data types
R data types
 
R factors
R   factorsR   factors
R factors
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With R
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Python list
Python listPython list
Python list
 
An Introduction to Data Mining with R
An Introduction to Data Mining with RAn Introduction to Data Mining with R
An Introduction to Data Mining with R
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in r
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
R programming presentation
R programming presentationR programming presentation
R programming presentation
 
R data types
R   data typesR   data types
R data types
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 

Similar to R Programming: Introduction to Matrices

R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
MemeMiner
 
Matlab-Data types and operators
Matlab-Data types and operatorsMatlab-Data types and operators
Matlab-Data types and operators
Luckshay Batra
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
Vikash Jakhar
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
JanineCallangan
 
Exercise1[5points]Create the following classe
Exercise1[5points]Create the following classeExercise1[5points]Create the following classe
Exercise1[5points]Create the following classe
mecklenburgstrelitzh
 
Pandas csv
Pandas csvPandas csv
Pandas csv
Devashish Kumar
 
1 linear algebra matrices
1 linear algebra matrices1 linear algebra matrices
1 linear algebra matrices
AmanSaeed11
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
Sabi995708
 
Matlab tutorial 1
Matlab tutorial 1Matlab tutorial 1
Matlab tutorial 1
Norhan Abdalla
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
Hamad Odhabi
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
karymadelaneyrenne19
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
Prabu U
 
Regression analysis in excel
Regression analysis in excelRegression analysis in excel
Regression analysis in excel
Awais Salman
 
Vba and macro creation (using excel)
Vba and macro creation (using excel)Vba and macro creation (using excel)
Vba and macro creation (using excel)
Javier Morales Cauna
 
Numerical analysis using Scilab: Numerical stability and conditioning
Numerical analysis using Scilab: Numerical stability and conditioningNumerical analysis using Scilab: Numerical stability and conditioning
Numerical analysis using Scilab: Numerical stability and conditioning
Scilab
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
Emertxe Information Technologies Pvt Ltd
 
C programming exercises and solutions
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions
Rumman Ansari
 
Abap Questions
Abap QuestionsAbap Questions
Abap Questions
Kaustav Pyne
 

Similar to R Programming: Introduction to Matrices (20)

R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 
Matlab-Data types and operators
Matlab-Data types and operatorsMatlab-Data types and operators
Matlab-Data types and operators
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
 
Exercise1[5points]Create the following classe
Exercise1[5points]Create the following classeExercise1[5points]Create the following classe
Exercise1[5points]Create the following classe
 
Pandas csv
Pandas csvPandas csv
Pandas csv
 
1 linear algebra matrices
1 linear algebra matrices1 linear algebra matrices
1 linear algebra matrices
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Matlab tutorial 1
Matlab tutorial 1Matlab tutorial 1
Matlab tutorial 1
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
 
Imp_Points_Scala
Imp_Points_ScalaImp_Points_Scala
Imp_Points_Scala
 
Regression analysis in excel
Regression analysis in excelRegression analysis in excel
Regression analysis in excel
 
Vba and macro creation (using excel)
Vba and macro creation (using excel)Vba and macro creation (using excel)
Vba and macro creation (using excel)
 
Numerical analysis using Scilab: Numerical stability and conditioning
Numerical analysis using Scilab: Numerical stability and conditioningNumerical analysis using Scilab: Numerical stability and conditioning
Numerical analysis using Scilab: Numerical stability and conditioning
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
C programming exercises and solutions
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions
 
Abap Questions
Abap QuestionsAbap Questions
Abap Questions
 

More from Rsquared Academy

Handling Date & Time in R
Handling Date & Time in RHandling Date & Time in R
Handling Date & Time in R
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Joining Data with dplyr
Joining Data with dplyrJoining Data with dplyr
Joining Data with dplyr
Rsquared Academy
 
Explore Data using dplyr
Explore Data using dplyrExplore Data using dplyr
Explore Data using dplyr
Rsquared Academy
 
Data Wrangling with dplyr
Data Wrangling with dplyrData Wrangling with dplyr
Data Wrangling with dplyr
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Introduction to tibbles
Introduction to tibblesIntroduction to tibbles
Introduction to tibbles
Rsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
Rsquared Academy
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into RRead/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
Rsquared Academy
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?
Rsquared Academy
 
How to get help in R?
How to get help in R?How to get help in R?
How to get help in R?
Rsquared Academy
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple Graphs
Rsquared Academy
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To Plots
Rsquared Academy
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersData Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Rsquared Academy
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsData Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of Plots
Rsquared Academy
 

More from Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in RHandling Date & Time in R
Handling Date & Time in R
 
Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
 
Joining Data with dplyr
Joining Data with dplyrJoining Data with dplyr
Joining Data with dplyr
 
Explore Data using dplyr
Explore Data using dplyrExplore Data using dplyr
Explore Data using dplyr
 
Data Wrangling with dplyr
Data Wrangling with dplyrData Wrangling with dplyr
Data Wrangling with dplyr
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
 
Introduction to tibbles
Introduction to tibblesIntroduction to tibbles
Introduction to tibbles
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into RRead/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?
 
How to get help in R?
How to get help in R?How to get help in R?
How to get help in R?
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple Graphs
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To Plots
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersData Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsData Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of Plots
 

Recently uploaded

06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Subhajit Sahu
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
AnirbanRoy608946
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 

Recently uploaded (20)

06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 

R Programming: Introduction to Matrices

  • 2. R2 AcademyCourse Material Slide 2 All the material related to this course are available on our website Scripts can be downloaded from GitHub Videos can be viewed on our Youtube Channel
  • 3. R2 AcademyObjectives Slide 3 ➢ Create matrices ➢ Matrix Operations ➢ Combine matrices ➢ Index/Subset matrix ➢ Dissolve matrix
  • 4. R2 Academy Slide 4 COLUMNS 1 2 3 R O 4 5 6 W S 7 8 9 10 11 12 A matrix is a rectangular array of data elements, arranged in rows and columns. Matrices in R are homogeneous i.e. they can hold a single type of data. Matrix elements are indexed by specifying the row and column index and the elements of a matrix can be filled by row or columns. In the first section, we look at various methods of creating matrices in R.
  • 5. R2 Academy Slide 5 The easiest way to create a matrix in R is to use the function. Let us look at its syntax: data Data Elements of the matrix Integer/Numeric/Logical/Character nrow Number of rows Integer ncol Number of columns Integer byrow Whether data should be filled by rows Logical dimnames Names of rows and columns Character vector
  • 6. R2 Academy Slide 6 Now that we have understood the syntax of the function, let us create a simple numeric matrix:
  • 7. R2 Academy Slide 7 In the previous example, we created a matrix of 3 rows where the data elements are filled by columns. We need to specify either the number of or and R will automatically calculate the other. The number of data elements should be equal to the product of number of rows and columns, else R will return an warning message.
  • 8. R2 Academy Slide 8 We can follow some general rules to avoid the mistakes made in the previous two examples: ● If the number of data elements are odd, both the number of rows and columns must be odd and their product should equal the number of data elements. ● If the number of data elements are even, either the number of rows must be even or the number of columns must be even. In certain cases, both of them must be even.
  • 9. R2 Academy Slide 9 Let us continue to explore the syntax of the function. Let us create two matrices, the data elements of which are filled by rows in the first case, and columns in the second case.
  • 10. R2 Academy Slide 10 Either the number of rows or columns need to be specified and R will calculate the other one automatically. We create two matrices below, the first one specified by rows and the second one by columns.
  • 11. R2 Academy Slide 11 If we want to specify the names of the rows and columns, we need to use a data structure called . Lists can contain other data structures including other lists. They are heterogeneous i.e. they can contain different data types. We will learn more about lists in the next unit. For now, let us learn to create a basic list. Lists in R can be created using the function.
  • 12. R2 Academy Slide 12 Let us now create a list of row and column names and use it to name the rows and columns of a matrix.
  • 13. R2 Academy Slide 13 Another method to create a matrix is to use the function. It is basically used to check or specify the dimensions of a data structure. In case of matrices, it returns the number of rows and columns. Let us look at a few examples:
  • 14. R2 Academy Slide 14 In the below example, we use the function to change the dimension of the matrix.In the dim function, we need to mention both the number of rows and columns using the function. We change the rows from 3 to 4 and the columns from 4 to 3.
  • 15. R2 Academy Slide 15 In the below example, we use the function to change row from 2 to 6 and the columns from 6 to 2.
  • 16. R2 Academy Slide 16 Now that we have understood the function, let us use it to convert vectors to matrices. Below are a few examples:
  • 18. R2 Academy Slide 18 The last method that we will explore in this section is the function. It is used to coerce a data structure to the type . Since the only other data structure that we have covered so far is the vector, we will coerce a vector to type We will deal with other data structures as and when we learn about them.
  • 19. R2 Academy Slide 19 Regardless of the data type of vector, all of them are coerced to a of dimension n x 1 i.e. they will all have only one column.
  • 20. R2 Academy Slide 20 In this section, we will cover the following: ● Matrix Addition ● Matrix Subtraction ● Matrix Division ● Transpose of a Matrix ● Matrix Multiplication ● Inverse of a Matrix The four basic operations of addition, subtraction, multiplication and division can be done by element wise or with a scalar value. We will be looking at both cases. In the case of multiplication, we need to compute the transpose of the matrix before we can do element wise multiplication.
  • 25. R2 Academy Slide 25 We can use the function to compute the transpose of a matrix.
  • 26. R2 Academy Slide 26 We need to follow the basic rules of matrix multiplication i.e. the number of columns in the first matrix should be equal to the number of rows in the second matrix. Let us look at an example:
  • 27. R2 Academy Slide 27 The inverse of a matrix can be computed using the function.
  • 28. R2 Academy Slide 28 In this section, we will focus on appending vector to matrices and combining matrices. There are two functions that can be used for this purpose: ● ● will combine/append the data by columns while will do the same by rows. When you use to combine two matrices, the number of columns must match and in case of , the number of rows must match.
  • 33. R2 Academy Slide 33 In this section, we will learn to index/subset elements of a matrix. The operator can be used to index the elements as we did in case of vectors but since matrices are two dimensional, we need to specify both the row number and the column number. Below are a few examples:
  • 36. R2 Academy Slide 36 In an earlier section, we learnt how to name the rows and columns of a matrix. Let us see how these names can be used to subset matrices.
  • 37. R2 Academy Slide 37 When you are using names of columns or rows for subsetting data from matrices, ensure that they are enclosed in single or double quotes.
  • 38. R2 Academy Slide 38 We can use logical expressions to subset elements of a matrix.
  • 39. R2 Academy Slide 39 So far, we have learnt how to coerce a vector to a matrix. In this final section, we will learn to coerce a matrix to a vector. We can use the following functions: ● ●
  • 40. R2 Academy Slide 40 ● Matrices are two dimensional arrays. ● They are homogeneous i.e. they can hold single type of data. ● The easiest way to create a matrix is by using the function. ● The function can be used to specify the dimensions of a matrix. ● They can be indexed using or names of rows/columns. ● Out of range index returns error. ● Negative index drops row/column from the matrix. ● Use function for transpose and function for inverse of a matrix. ● and can be used to append vectors and combine matrices. ● Logical expressions can be used to subset matrices.
  • 41. R2 AcademyNext Steps... Slide 41 In the next module: ✓ Construct Lists ✓ Describe Lists ✓ Index/Subset List Elements ✓ Convert Lists
  • 42. R2 Academy Slide 42 Visit Rsquared Academy for tutorials on: → R Programming → Business Analytics → Data Visualization → Web Applications → Package Development → Git & GitHub