0% found this document useful (0 votes)
0 views4 pages

2.2 matrix

The document provides an overview of matrices in R, including their creation, manipulation, and basic operations. It explains how to create matrices using the matrix() function, bind vectors, name rows and columns, extract elements, and perform arithmetic operations. Additionally, it covers matrix computation, including scalar multiplication, matrix addition, and the use of functions like apply() for row and column operations.

Uploaded by

2025508609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

2.2 matrix

The document provides an overview of matrices in R, including their creation, manipulation, and basic operations. It explains how to create matrices using the matrix() function, bind vectors, name rows and columns, extract elements, and perform arithmetic operations. Additionally, it covers matrix computation, including scalar multiplication, matrix addition, and the use of functions like apply() for row and column operations.

Uploaded by

2025508609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Matrix

In R, matrices are two-dimensional (rows x column) that store data. Each element in a matrix
must be of the same data type (all numeric or all character). A matrix is created using the
matrix() function. By default, matrices are constructed column-wise.

matrix(data, nrow, ncol, byrow = T or F, dimnames = list())

x <- matrix(1:6, nrow = 2, ncol = 3) # by default: the element are fill by column-wise
x

x<-matrix(1:12, ncol=3, byrow=T) # To fill row-wise, we specify byrow=T


x

Matrices can also be created directly from vectors by adding a dimension attribute dim()
x <- 1:10
x
[1] 1 2 3 4 5 6 7 8 9 10
dim(x) <- c(2, 5) # no of row=2, no of column=5
x

Create matrix by combining the vectors.


Matrices also can be created by column-binding or row-binding with the cbind() and rbind()
functions.
Example;
x <- 1:3
y <- 10:12
cbind(x, y) #column-binding > rbind(x, y) #row-binding
x y [,1] [,2] [,3]
[1,] 1 10 x 1 2 3
[2,] 2 11 y 10 11 12
[3,] 3 12

Example;
x<-1:4
y<-10:13
z<-21:24
cbind(x,y,z) rbind(x,y,z)
x y z [,1] [,2] [,3] [,4]
[1,] 1 10 21 x 1 2 3 4
[2,] 2 11 22 y 10 11 12 13
[3,] 3 12 23 z 21 22 23 24
[4,] 4 13 24
Naming the row and column for matrix

Naming the column and row of matrix using dimnames()


namarow = c("Row1", "Row2", "Row3")
namacolumn = c("Col1", "Col2", "Col3")
AA <- matrix(1:9, nrow = 3, dimnames = list(namarow, namacolumn))
AA
Col1 Col2 Col3
Row1 1 4 7
Row2 2 5 8
Row3 3 6 9

r= c("Row1", "Row2", "Row3")


c= paste("col", c("1","2","3"), sep="-")
AA <- matrix(1:9, nrow = 3, dimnames = list(r, c))
AA

Naming the column and row of matrix using colnames() and rownames()
AA<-matrix(10:30, ncol=3)
AA
colnames(AA)<-c("var1","var2","var3")
AA
rownames(AA)<-c("p1","p2","p3","p4","p5","p6","p7")
AA

Or can combine the command using semicolons;


AA<-matrix(10:30, ncol=3); colnames(AA)<-c("var1","var2","var3"); rownames(AA)<-
c("p1","p2","p3","p4","p5","p6","p7")
AA
Extracting the element of a matrix [row,column]
x<-c(1:12)
AA<-matrix(x,ncol=4)
AA
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

AA[1,3] # select row1,column3


AA[-1,3] # exclude row1, select column3
AA[-1,-3] # exclude row1 and column3
AA[c(1,3),-3] # select row 1 and 3,exclude column3

M<-matrix(1:25,ncol=5)
M
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25

M[c(1,3,5),] # select row1,3,5 and include all column


M[-c(1,3,5),] # remove row 1,3,5, include all column
M[,c(1,2,4)] # include all row, and select column 1,2,4
M[,c(-1,-2,-4)] # select all row, and remove column 1,2,4

Matrix computation

We can perform arithmetic operations on matrices just like vectors, element-wise or using
matrix multiplication.

AA<-matrix(1:4, ncol=2) BB<-matrix(c(12,14,15,17),ncol=2)


AA BB
[,1] [,2] [,1] [,2]
[1,] 1 3 [1,] 12 15
[2,] 2 4 [2,] 14 17

2*AA # scalar multiplication


AA+BB # matrix addition
AA*BB # component-wise multiplication.

Note that AA*BB is NOT the usual matrix multiplication. To do the real matrix multiplication,
use %*%

Multiplication (use %*% instead of *)


AA*BB AA%*%BB

[,1] [,2] view the difference [,1] [,2]


[1,] 12 45 [1,] 54 66
[2,] 28 68 [2,] 80 98
solve(AA) #Inverse matrix
[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5

t(AA) # Transpose of matrix

solve(AA)%*%AA # M multiply with M-1 is identity matrix


[,1] [,2]
[1,] 1 0
[2,] 0 1

diag(AA) # diagonal value for AA


[1] 1 4

diag(3) # create identity matrix


diag(6)
diag(c(2,3,3)) # create matrix with diagonal value of 2,3,3
[,1] [,2] [,3]
[1,] 2 0 0
[2,] 0 3 0
[3,] 0 0 3

Apply a function to each row or column in matrix


We can easyly find sum or means of each row and column in the matrix using;

x<-c(70,65,120,140)
dataset<-matrix(x, nrow=2, ncol=2)
rownames(dataset)<-c("male","female")
colnames(dataset)<-c("great","excellent")
dataset

colMeans(dataset)
colSums(dataset)
rowMeans(dataset)
rowSums(dataset)

We can use apply() to apply functions like sum(), mean(), max(), sd(), and more to matrices.
This is helpful for performing operations across specific rows or columns of a matrix. We'll
cover the apply family functions in a later chapter.

You might also like