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 (20)

PPTX
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
PPTX
Non Linear Data Structures
Adarsh Patel
 
PPTX
Overview of UML Diagrams
Manish Kumar
 
PDF
Python Decision Making
Soba Arjun
 
PPT
12. Indexing and Hashing in DBMS
koolkampus
 
PDF
Array data structure
maamir farooq
 
PPTX
Data reduction
kalavathisugan
 
PPTX
Relational Data Model Introduction
Nishant Munjal
 
PPTX
ER MODEL
Rupali Rana
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
linked list in data structure
shameen khan
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PDF
Import Data using R
Rupak Roy
 
PPT
Abstract data types
Poojith Chowdhary
 
PDF
Python tuple
Mohammed Sikander
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPTX
Computer Science-Data Structures :Abstract DataType (ADT)
St Mary's College,Thrissur,Kerala
 
PPTX
Type casting
Ruchika Sinha
 
PPTX
Types of Statements in Python Programming Language
Explore Skilled
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
Non Linear Data Structures
Adarsh Patel
 
Overview of UML Diagrams
Manish Kumar
 
Python Decision Making
Soba Arjun
 
12. Indexing and Hashing in DBMS
koolkampus
 
Array data structure
maamir farooq
 
Data reduction
kalavathisugan
 
Relational Data Model Introduction
Nishant Munjal
 
ER MODEL
Rupali Rana
 
Class and Objects in Java
Spotle.ai
 
linked list in data structure
shameen khan
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Import Data using R
Rupak Roy
 
Abstract data types
Poojith Chowdhary
 
Python tuple
Mohammed Sikander
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Computer Science-Data Structures :Abstract DataType (ADT)
St Mary's College,Thrissur,Kerala
 
Type casting
Ruchika Sinha
 
Types of Statements in Python Programming Language
Explore Skilled
 

Similar to R Programming: Introduction to Matrices (20)

PDF
R Programming: Introduction to Vectors
Rsquared Academy
 
DOCX
R Matrix Math Quick Reference
Mark Niemann-Ross
 
PPTX
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
 
PPT
R Programming Intro
062MayankSinghal
 
PPT
R tutorial for a windows environment
Yogendra Chaubey
 
PDF
Advanced MATLAB Tutorial for Engineers & Scientists
Ray Phan
 
PDF
2 data structure in R
naroranisha
 
PDF
3 Data Structure in R
Dr Nisha Arora
 
PPTX
R Basics
Dr.E.N.Sathishkumar
 
PPTX
Mat lab.pptx
MeshackDuru
 
PPT
Matlab introduction
Vikash Jakhar
 
PPTX
R data-structures-3
Victor Ordu
 
PDF
Matrix algebra in_r
Razzaqe
 
PPTX
Matlab matrices and arrays
Ameen San
 
PPTX
Introduction to R.pptx
RohithK65
 
PPTX
Big Data Mining in Indian Economic Survey 2017
Parth Khare
 
PDF
Matlab tutorial 2
Norhan Abdalla
 
PDF
Matlab quick quide3.4
Mohamd Fawzi Nabulsi
 
PPTX
Matrices(teaching)Matrices(teaching)Matrices(teaching)Matrices(teaching)Matri...
Biotech23
 
PPTX
Matlab Tutorial
Ahmad Siddiq
 
R Programming: Introduction to Vectors
Rsquared Academy
 
R Matrix Math Quick Reference
Mark Niemann-Ross
 
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
 
R Programming Intro
062MayankSinghal
 
R tutorial for a windows environment
Yogendra Chaubey
 
Advanced MATLAB Tutorial for Engineers & Scientists
Ray Phan
 
2 data structure in R
naroranisha
 
3 Data Structure in R
Dr Nisha Arora
 
Mat lab.pptx
MeshackDuru
 
Matlab introduction
Vikash Jakhar
 
R data-structures-3
Victor Ordu
 
Matrix algebra in_r
Razzaqe
 
Matlab matrices and arrays
Ameen San
 
Introduction to R.pptx
RohithK65
 
Big Data Mining in Indian Economic Survey 2017
Parth Khare
 
Matlab tutorial 2
Norhan Abdalla
 
Matlab quick quide3.4
Mohamd Fawzi Nabulsi
 
Matrices(teaching)Matrices(teaching)Matrices(teaching)Matrices(teaching)Matri...
Biotech23
 
Matlab Tutorial
Ahmad Siddiq
 
Ad

More from Rsquared Academy (20)

PDF
Handling Date & Time in R
Rsquared Academy
 
PDF
Market Basket Analysis in R
Rsquared Academy
 
PDF
Practical Introduction to Web scraping using R
Rsquared Academy
 
PDF
Joining Data with dplyr
Rsquared Academy
 
PDF
Explore Data using dplyr
Rsquared Academy
 
PDF
Data Wrangling with dplyr
Rsquared Academy
 
PDF
Writing Readable Code with Pipes
Rsquared Academy
 
PDF
Introduction to tibbles
Rsquared Academy
 
PDF
Read data from Excel spreadsheets into R
Rsquared Academy
 
PDF
Read/Import data from flat/delimited files into R
Rsquared Academy
 
PDF
Variables & Data Types in R
Rsquared Academy
 
PDF
How to install & update R packages?
Rsquared Academy
 
PDF
How to get help in R?
Rsquared Academy
 
PDF
Introduction to R
Rsquared Academy
 
PDF
RMySQL Tutorial For Beginners
Rsquared Academy
 
PDF
R Markdown Tutorial For Beginners
Rsquared Academy
 
PDF
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
PDF
Data Visualization With R: Learn To Combine Multiple Graphs
Rsquared Academy
 
PDF
R Data Visualization: Learn To Add Text Annotations To Plots
Rsquared Academy
 
PDF
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Rsquared Academy
 
Handling Date & Time in R
Rsquared Academy
 
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Rsquared Academy
 
Joining Data with dplyr
Rsquared Academy
 
Explore Data using dplyr
Rsquared Academy
 
Data Wrangling with dplyr
Rsquared Academy
 
Writing Readable Code with Pipes
Rsquared Academy
 
Introduction to tibbles
Rsquared Academy
 
Read data from Excel spreadsheets into R
Rsquared Academy
 
Read/Import data from flat/delimited files into R
Rsquared Academy
 
Variables & Data Types in R
Rsquared Academy
 
How to install & update R packages?
Rsquared Academy
 
How to get help in R?
Rsquared Academy
 
Introduction to R
Rsquared Academy
 
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
Data Visualization With R: Learn To Combine Multiple Graphs
Rsquared Academy
 
R Data Visualization: Learn To Add Text Annotations To Plots
Rsquared Academy
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Rsquared Academy
 
Ad

Recently uploaded (20)

PDF
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
PPTX
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
PDF
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
PPT
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PDF
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PDF
Research Methodology Overview Introduction
ayeshagul29594
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
PDF
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
PPTX
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PDF
apidays Singapore 2025 - Streaming Lakehouse with Kafka, Flink and Iceberg by...
apidays
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PDF
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
PDF
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
PDF
The Best NVIDIA GPUs for LLM Inference in 2025.pdf
Tamanna36
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
Research Methodology Overview Introduction
ayeshagul29594
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
apidays Singapore 2025 - Streaming Lakehouse with Kafka, Flink and Iceberg by...
apidays
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
The Best NVIDIA GPUs for LLM Inference in 2025.pdf
Tamanna36
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 

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