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

PPTX
2. R-basics, Vectors, Arrays, Matrices, Factors
PPTX
3. R- list and data frame
PDF
R Programming: Introduction To R Packages
PPT
R studio
PPTX
Exception Handling in object oriented programming using C++
PPTX
Unit 1 - R Programming (Part 2).pptx
PPTX
Introduction to data structure ppt
2. R-basics, Vectors, Arrays, Matrices, Factors
3. R- list and data frame
R Programming: Introduction To R Packages
R studio
Exception Handling in object oriented programming using C++
Unit 1 - R Programming (Part 2).pptx
Introduction to data structure ppt

What's hot (20)

PPT
Lecture 1 data structures and algorithms
PPT
Python Pandas
PDF
R Programming: Mathematical Functions In R
PPTX
Stack and Queue
PPTX
Data structure power point presentation
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Data structure - Graph
PDF
Life cycle-of-a-thread
PPTX
Data visualization using R
PPTX
Dimensionality Reduction and feature extraction.pptx
PPT
Memory allocation in c
PPT
Spanning trees
PPT
3.1 clustering
PPTX
Applications of DBMS(Database Management System)
PDF
Algorithms Lecture 7: Graph Algorithms
PDF
Python programming : Files
PPTX
Data Structures (CS8391)
PDF
Python set
PDF
Decision trees in Machine Learning
PPTX
Graph representation
Lecture 1 data structures and algorithms
Python Pandas
R Programming: Mathematical Functions In R
Stack and Queue
Data structure power point presentation
BINARY TREE REPRESENTATION.ppt
Data structure - Graph
Life cycle-of-a-thread
Data visualization using R
Dimensionality Reduction and feature extraction.pptx
Memory allocation in c
Spanning trees
3.1 clustering
Applications of DBMS(Database Management System)
Algorithms Lecture 7: Graph Algorithms
Python programming : Files
Data Structures (CS8391)
Python set
Decision trees in Machine Learning
Graph representation
Ad

Similar to R Programming: Introduction to Matrices (20)

PDF
R Programming: Introduction to Vectors
PPTX
R Programming: Variables & Data Types
PDF
Data Structure.pdf
PDF
Matlab-Data types and operators
PPT
Matlab introduction
PPTX
c++ arrays and pointers grade 9 STEP curriculum.pptx
DOCX
Exercise1[5points]Create the following classe
PPTX
Pandas csv
PDF
1 linear algebra matrices
PPTX
C++.pptx
PDF
Matlab tutorial 1
PDF
CIS 1403 lab 3 functions and methods in Java
DOCX
3rd-Sem_CSE_Data-Structures and Applications.docx
PDF
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
PDF
Data Visualization With R
PPTX
Arrays with Numpy, Computer Graphics
PPTX
BEN520 FUNDAMENTALS OF BOENGINEERING II-4 week-lecture 4.pptx
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
RTF
Imp_Points_Scala
PDF
Regression analysis in excel
R Programming: Introduction to Vectors
R Programming: Variables & Data Types
Data Structure.pdf
Matlab-Data types and operators
Matlab introduction
c++ arrays and pointers grade 9 STEP curriculum.pptx
Exercise1[5points]Create the following classe
Pandas csv
1 linear algebra matrices
C++.pptx
Matlab tutorial 1
CIS 1403 lab 3 functions and methods in Java
3rd-Sem_CSE_Data-Structures and Applications.docx
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Data Visualization With R
Arrays with Numpy, Computer Graphics
BEN520 FUNDAMENTALS OF BOENGINEERING II-4 week-lecture 4.pptx
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Imp_Points_Scala
Regression analysis in excel
Ad

More from Rsquared Academy (20)

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

Recently uploaded (20)

PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
Mega Projects Data Mega Projects Data
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
PPTX
Global journeys: estimating international migration
PDF
Data Science Trends & Career Guide---ppt
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PPTX
1_Introduction to advance data techniques.pptx
PPT
Quality review (1)_presentation of this 21
PDF
Oracle OFSAA_ The Complete Guide to Transforming Financial Risk Management an...
PDF
The Rise of Impact Investing- How to Align Profit with Purpose
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
Foundation of Data Science unit number two notes
PPTX
Moving the Public Sector (Government) to a Digital Adoption
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Mega Projects Data Mega Projects Data
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
Global journeys: estimating international migration
Data Science Trends & Career Guide---ppt
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
1_Introduction to advance data techniques.pptx
Quality review (1)_presentation of this 21
Oracle OFSAA_ The Complete Guide to Transforming Financial Risk Management an...
The Rise of Impact Investing- How to Align Profit with Purpose
Introduction-to-Cloud-ComputingFinal.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
.pdf is not working space design for the following data for the following dat...
Supervised vs unsupervised machine learning algorithms
Foundation of Data Science unit number two notes
Moving the Public Sector (Government) to a Digital Adoption
climate analysis of Dhaka ,Banglades.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.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