SlideShare a Scribd company logo
Working With Matrices in R
Dr. T. N. Kavitha
Assistant Professor of Mathematics
SCSVMV
To create a vector in R
• > vec <- c( 1.9, -2.8, 5.6, 0, 3.4, -4.2 )
• Then the vector looks like:
• > vec
• [1] 1.9 -2.8 5.6 0.0 3.4 -4.2
2
Dr T N Kavitha
To make a sequence of integers
• > vec <- -2:5
• Then the vector looks like:
• > vec
• [1] -2 -1 0 1 2 3 4 5
• > vec <- 5:-2
• Then we get:
• > vec
• [1] 5 4 3 2 1 0 -1 -2
3
Dr T N Kavitha
To create the sequence function
• We have to give a starting value, and ending value, and an
increment value, e.g.,
• > vec <- seq( -3.1, 2.2, by=0.1 )
• Then we get:
• > vec
• [1] -3.1 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7
• [16] -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2
• [31] -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3
• [46] 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2
Note that R wraps the vector around to four rows, and at the beginning of each
row gives you the position in the vector of the first element of the row. We can
run a sequence backward, from a larger value to a smaller value, but we must
use a negative increment value.
4
Dr T N Kavitha
To find the replicate function to a get a
vector whose elements are all the same
• > vec <- rep( 1, 8 )
• Then we get:
• > vec
• [1] 1 1 1 1 1 1 1 1
That is, we get 1 replicated 8 times.
• two or more vectors to make a larger vector, e.g.,
• > vec <- c( rep(2,4), 1:3, c(8,1,9) )
• Then we get
• > vec
• [1] 2 2 2 2 1 2 3 8 1 9
5
Dr T N Kavitha
To know how many elements are in the
vector
• > length(vec)
If we want to reference an element in the vector,
we use square brackets, e.g.,
• > vec[5]
This will give us the fifth element of the vector vec.
• To reference a consecutive subset of the vector,
we use the colon operator within the square
brackets, e.g.,
• > vec[2:7]
• This will give us elements two through seven of
the vector vec.
6
Dr T N Kavitha
To make a 2 x 3 matrix named M consisting
of the integers 1 through 6
• > M <- matrix( 1:6, nrow=2 )
• or this:
• > M <- matrix( 1:6, ncol=3 )
• We get:
• > M
• [,1] [,2] [,3]
• [1,] 1 3 5
• [2,] 2 4 6
• Note that R places the row numbers on the left
and the column numbers on top. Also note that R
filled in the matrix column-by-column.
7
Dr T N Kavitha
If we prefer to fill in the matrix row-by-row,
we must activate the byrow setting, e.g.,
• > M <- matrix( 1:6, ncol=3 )
• Then we get:
• > M
• [,1] [,2] [,3]
• [1,] 1 2 3
• [2,] 4 5 6
In place of the vector 1:6 you would place
any vector containing the desired elements of
the matrix.
8
Dr T N Kavitha
To obtain the transpose of a matrix
• > t(M)
• This gives us the transpose of the matrix
M created above:
• > t(M)
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
9
Dr T N Kavitha
To add or subtract two matrices (or vectors)
which have the same number of rows and
columns
• we use the plus and minus symbols, e.g.,
• > A + B
• > A - B
• To multiply two matrices with compatible dimensions
• > A %*% B
If we just use the multiplication operator *, R will multiply
the corresponding elements of the two matrices,
provided they have the same dimensions.
• To multiply two vectors to get their scalar (inner)
product, we use the same operator, e.g.,
• > a %*% b
• R will transpose a for us rather than giving us an error
message.
10
Dr T N Kavitha
To create the identity matrix for a desired
dimension
• > I <- diag(5)
• This gives us the 5 x 5 identity matrix:
• > I
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
11
Dr T N Kavitha
To find the determinant of a square matrix N
• To find the determinant of a square matrix
N, use the determinant function, e.g.,
• > det( N )
12
Dr T N Kavitha
To obtain the inverse N-1 of an invertible
square matrix N
• > solve( N )
• If the matrix is singular (not invertible), or almost
singular, we get an error message.
• A <- as.matrix(cbind(c(3,0),c(1,2)))
• A
• A1 <- matrix.inverse(A)
• A1
• solve(A)
• A %*% A1
• [,1] [,2]
• ## [1,] 1 0
## [2,] 0 1
13
Dr T N Kavitha
>mp <-
matrix(c(0,1/4,1/4,3/4,0,1/4,1/4,3/4,1/2),3,
3,byrow=T)
> mp
>eigen(mp)
> $vectors
Øsolve(eigen(mp)$vectors)
14
Dr T N Kavitha
TUTORIAL
• https://www.geeksforgeeks.org/find-
eigenvalues-and-eigenvectors-of-a-matrix-
in-r-programming-eigen-function/
15
Dr T N Kavitha
16
Dr T N Kavitha

More Related Content

PDF
MATLAB Basics-Part1
PDF
Introduction to MATLAB 1
PPTX
Introduction to MATLAB
PDF
Matlab HTI summer training course_Lecture2
PDF
Matlab for beginners, Introduction, signal processing
PPT
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
PDF
Introduction to simulink (1)
PPT
Matlab practical and lab session
MATLAB Basics-Part1
Introduction to MATLAB 1
Introduction to MATLAB
Matlab HTI summer training course_Lecture2
Matlab for beginners, Introduction, signal processing
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
Introduction to simulink (1)
Matlab practical and lab session

What's hot (20)

PPT
Introduction to matlab
PDF
Matlab-Data types and operators
PPT
Matlab Introduction
PDF
Matlab programming project
PPTX
Matlab HTI summer training course Lecture3
PDF
Introduction to Matlab
PDF
Simulink
PPT
Basics of programming in matlab
PPTX
Matlab for diploma students(1)
PDF
Advanced MATLAB Tutorial for Engineers & Scientists
PDF
Matlab solved problems
DOCX
B61301007 matlab documentation
PDF
Matlab from Beginner to Expert
PPTX
Importance of matlab
PPTX
Basic matlab and matrix
PPT
Matlab introduction
PPTX
MATLAB - The Need to Know Basics
PDF
Forelasning4
PPT
Introduction to matlab
PPT
Introduction to MatLab programming
Introduction to matlab
Matlab-Data types and operators
Matlab Introduction
Matlab programming project
Matlab HTI summer training course Lecture3
Introduction to Matlab
Simulink
Basics of programming in matlab
Matlab for diploma students(1)
Advanced MATLAB Tutorial for Engineers & Scientists
Matlab solved problems
B61301007 matlab documentation
Matlab from Beginner to Expert
Importance of matlab
Basic matlab and matrix
Matlab introduction
MATLAB - The Need to Know Basics
Forelasning4
Introduction to matlab
Introduction to MatLab programming
Ad

Similar to working with matrices in r (20)

PPTX
Vectormaths and Matrix in R.pptx
PPTX
R Programming and Lab - Unit II with examples
PDF
Matrix algebra in_r
PDF
Basics of Matlab for students and faculty
PPTX
MATLAB - Arrays and Matrices
PPTX
Vectors.pptx
PPT
Matlab Tutorial.ppt
PPTX
Introduction to Matlab and application.pptx
PPTX
Matlab ch1 (3)
PPT
Matlab an Introduction_Lecture_for all.ppt
PDF
Class13_Quicksort_Algorithm.pdf
PPT
Introduction to Matlab - Basic Functions
PDF
[1062BPY12001] Data analysis with R / week 2
PPT
Lca seminar modified
PPT
473431331-Matlab-Simulink-Tutorial-ppt.ppt
PDF
CE344L-200365-Lab2.pdf
PDF
C Language Lecture 10
PPTX
COMPANION TO MATRICES SESSION II.pptx
Vectormaths and Matrix in R.pptx
R Programming and Lab - Unit II with examples
Matrix algebra in_r
Basics of Matlab for students and faculty
MATLAB - Arrays and Matrices
Vectors.pptx
Matlab Tutorial.ppt
Introduction to Matlab and application.pptx
Matlab ch1 (3)
Matlab an Introduction_Lecture_for all.ppt
Class13_Quicksort_Algorithm.pdf
Introduction to Matlab - Basic Functions
[1062BPY12001] Data analysis with R / week 2
Lca seminar modified
473431331-Matlab-Simulink-Tutorial-ppt.ppt
CE344L-200365-Lab2.pdf
C Language Lecture 10
COMPANION TO MATRICES SESSION II.pptx
Ad

Recently uploaded (20)

PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Digital Strategies for Manufacturing Companies
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
history of c programming in notes for students .pptx
PDF
System and Network Administraation Chapter 3
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
ai tools demonstartion for schools and inter college
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
L1 - Introduction to python Backend.pptx
Design an Analysis of Algorithms I-SECS-1021-03
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Upgrade and Innovation Strategies for SAP ERP Customers
Digital Strategies for Manufacturing Companies
2025 Textile ERP Trends: SAP, Odoo & Oracle
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
history of c programming in notes for students .pptx
System and Network Administraation Chapter 3
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
How to Migrate SBCGlobal Email to Yahoo Easily
Operating system designcfffgfgggggggvggggggggg
Reimagine Home Health with the Power of Agentic AI​
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
ai tools demonstartion for schools and inter college
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Which alternative to Crystal Reports is best for small or large businesses.pdf
L1 - Introduction to python Backend.pptx

working with matrices in r

  • 1. Working With Matrices in R Dr. T. N. Kavitha Assistant Professor of Mathematics SCSVMV
  • 2. To create a vector in R • > vec <- c( 1.9, -2.8, 5.6, 0, 3.4, -4.2 ) • Then the vector looks like: • > vec • [1] 1.9 -2.8 5.6 0.0 3.4 -4.2 2 Dr T N Kavitha
  • 3. To make a sequence of integers • > vec <- -2:5 • Then the vector looks like: • > vec • [1] -2 -1 0 1 2 3 4 5 • > vec <- 5:-2 • Then we get: • > vec • [1] 5 4 3 2 1 0 -1 -2 3 Dr T N Kavitha
  • 4. To create the sequence function • We have to give a starting value, and ending value, and an increment value, e.g., • > vec <- seq( -3.1, 2.2, by=0.1 ) • Then we get: • > vec • [1] -3.1 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 • [16] -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 • [31] -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 • [46] 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 Note that R wraps the vector around to four rows, and at the beginning of each row gives you the position in the vector of the first element of the row. We can run a sequence backward, from a larger value to a smaller value, but we must use a negative increment value. 4 Dr T N Kavitha
  • 5. To find the replicate function to a get a vector whose elements are all the same • > vec <- rep( 1, 8 ) • Then we get: • > vec • [1] 1 1 1 1 1 1 1 1 That is, we get 1 replicated 8 times. • two or more vectors to make a larger vector, e.g., • > vec <- c( rep(2,4), 1:3, c(8,1,9) ) • Then we get • > vec • [1] 2 2 2 2 1 2 3 8 1 9 5 Dr T N Kavitha
  • 6. To know how many elements are in the vector • > length(vec) If we want to reference an element in the vector, we use square brackets, e.g., • > vec[5] This will give us the fifth element of the vector vec. • To reference a consecutive subset of the vector, we use the colon operator within the square brackets, e.g., • > vec[2:7] • This will give us elements two through seven of the vector vec. 6 Dr T N Kavitha
  • 7. To make a 2 x 3 matrix named M consisting of the integers 1 through 6 • > M <- matrix( 1:6, nrow=2 ) • or this: • > M <- matrix( 1:6, ncol=3 ) • We get: • > M • [,1] [,2] [,3] • [1,] 1 3 5 • [2,] 2 4 6 • Note that R places the row numbers on the left and the column numbers on top. Also note that R filled in the matrix column-by-column. 7 Dr T N Kavitha
  • 8. If we prefer to fill in the matrix row-by-row, we must activate the byrow setting, e.g., • > M <- matrix( 1:6, ncol=3 ) • Then we get: • > M • [,1] [,2] [,3] • [1,] 1 2 3 • [2,] 4 5 6 In place of the vector 1:6 you would place any vector containing the desired elements of the matrix. 8 Dr T N Kavitha
  • 9. To obtain the transpose of a matrix • > t(M) • This gives us the transpose of the matrix M created above: • > t(M) [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 9 Dr T N Kavitha
  • 10. To add or subtract two matrices (or vectors) which have the same number of rows and columns • we use the plus and minus symbols, e.g., • > A + B • > A - B • To multiply two matrices with compatible dimensions • > A %*% B If we just use the multiplication operator *, R will multiply the corresponding elements of the two matrices, provided they have the same dimensions. • To multiply two vectors to get their scalar (inner) product, we use the same operator, e.g., • > a %*% b • R will transpose a for us rather than giving us an error message. 10 Dr T N Kavitha
  • 11. To create the identity matrix for a desired dimension • > I <- diag(5) • This gives us the 5 x 5 identity matrix: • > I [,1] [,2] [,3] [,4] [,5] [1,] 1 0 0 0 0 [2,] 0 1 0 0 0 [3,] 0 0 1 0 0 [4,] 0 0 0 1 0 [5,] 0 0 0 0 1 11 Dr T N Kavitha
  • 12. To find the determinant of a square matrix N • To find the determinant of a square matrix N, use the determinant function, e.g., • > det( N ) 12 Dr T N Kavitha
  • 13. To obtain the inverse N-1 of an invertible square matrix N • > solve( N ) • If the matrix is singular (not invertible), or almost singular, we get an error message. • A <- as.matrix(cbind(c(3,0),c(1,2))) • A • A1 <- matrix.inverse(A) • A1 • solve(A) • A %*% A1 • [,1] [,2] • ## [1,] 1 0 ## [2,] 0 1 13 Dr T N Kavitha
  • 14. >mp <- matrix(c(0,1/4,1/4,3/4,0,1/4,1/4,3/4,1/2),3, 3,byrow=T) > mp >eigen(mp) > $vectors Øsolve(eigen(mp)$vectors) 14 Dr T N Kavitha
  • 16. 16 Dr T N Kavitha