SlideShare a Scribd company logo
1 of 42
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

Machine Learning-Linear regression
Machine Learning-Linear regressionMachine Learning-Linear regression
Machine Learning-Linear regressionkishanthkumaar
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to VectorsRsquared Academy
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
Exploratory data analysis with Python
Exploratory data analysis with PythonExploratory data analysis with Python
Exploratory data analysis with PythonDavis David
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data TypesRsquared Academy
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
Data preprocessing using Machine Learning
Data  preprocessing using Machine Learning Data  preprocessing using Machine Learning
Data preprocessing using Machine Learning Gopal Sakarkar
 
Python Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaPython Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaEdureka!
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset PreparationAndrew Ferlitsch
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna universitysangeethajames07
 
Introduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsIntroduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsPhoenix
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaEdureka!
 

What's hot (20)

Machine Learning-Linear regression
Machine Learning-Linear regressionMachine Learning-Linear regression
Machine Learning-Linear regression
 
R Programming
R ProgrammingR Programming
R Programming
 
R studio
R studio R studio
R studio
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
Python pandas Library
Python pandas LibraryPython pandas Library
Python pandas Library
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
 
Exploratory data analysis with Python
Exploratory data analysis with PythonExploratory data analysis with Python
Exploratory data analysis with Python
 
Step By Step Guide to Learn R
Step By Step Guide to Learn RStep By Step Guide to Learn R
Step By Step Guide to Learn R
 
Dbms architecture
Dbms architectureDbms architecture
Dbms architecture
 
Class ppt intro to r
Class ppt intro to rClass ppt intro to r
Class ppt intro to r
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Data preprocessing using Machine Learning
Data  preprocessing using Machine Learning Data  preprocessing using Machine Learning
Data preprocessing using Machine Learning
 
Python Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaPython Collections Tutorial | Edureka
Python Collections Tutorial | Edureka
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset Preparation
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 
Introduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsIntroduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data Analytics
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | Edureka
 

Similar to R Programming: Introduction to Matrices

Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdfMemeMiner
 
Matlab-Data types and operators
Matlab-Data types and operatorsMatlab-Data types and operators
Matlab-Data types and operatorsLuckshay Batra
 
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.pptxJanineCallangan
 
Exercise1[5points]Create the following classe
Exercise1[5points]Create the following classeExercise1[5points]Create the following classe
Exercise1[5points]Create the following classemecklenburgstrelitzh
 
1 linear algebra matrices
1 linear algebra matrices1 linear algebra matrices
1 linear algebra matricesAmanSaeed11
 
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 JavaHamad 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.pdfkarymadelaneyrenne19
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsPrabu U
 
Regression analysis in excel
Regression analysis in excelRegression analysis in excel
Regression analysis in excelAwais 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 conditioningScilab
 
C programming exercises and solutions
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions Rumman Ansari
 

Similar to R Programming: Introduction to Matrices (20)

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
 
Abap
AbapAbap
Abap
 

More from Rsquared Academy

Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in RRsquared 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 RRsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with PipesRsquared 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 RRsquared 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 RRsquared Academy
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in RRsquared 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
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersRsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsRsquared 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 GraphsRsquared 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 PlotsRsquared 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 ParametersRsquared 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
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in 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
 

Recently uploaded

B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一ffjhghh
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 

Recently uploaded (20)

B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 

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