An R matrix corresponds to a rectangular array of numbers. Technically, a matrix is a vector, but with two additional attributes: the number of rows and the number of columns. Technically, a matrix is a vector, but with two additional attributes: the number of rows and the number of columns

1. Different ways of creating matrix.

1.1 Using rbind (row bind function)

a<-rbind(c(1,2,3),c(4,5,6)) # Two vectors are binded to form a matrix row by row.
a
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6

1.2 Using cbind (column bind function)

b<-cbind(c(1,2,3),c(4,5,6)) # Two vectors are binded to form a matrix column by column.
b
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6

1.3 Using the matrix function.

d<-matrix(c(1,2,3,4,5,6), nrow = 3,ncol = 2) # nrow specify number of rows and #ncol specify number of columns
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6

Strictly speaking, there is no need to specify both number of rows and number of columns. R can understand if any one is given.

d<-matrix(c(1,2,3,4,5,6), nrow = 3)
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6

We can also create the matrix first and then give the elements later.

d<-matrix(nrow = 2,ncol = 2) # in this case both rows and columns should be specified.

d[1,1]<-1 # 1st row 1st element
d[2,1]<-2 # 2st row 1st element
d[1,2]<-3 # 1st row 2st element
d[2,2]<-4 # 2st row 2st element

d
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

Though internal storage of a matrix is in column-major order, you can set the byrow argument in matrix() to true to indicate that the data is coming in row-major order.

d<-matrix(c(1,2,3,4,5,6), nrow = 3,byrow = TRUE)
d
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## [3,]    5    6



2.Accessing elements in a matrix

Note that the matrix is accessed by a square bracket with 2 entities for example, d[row,column].


2.1 Accessing an element from a matrix

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
d[3,2] # Accessing 3rd row 2nd column element
## [1] 6

2.2 Accessing a row from a matrix

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
d[1,] # If we don't give any column value, then whole column values will be retrived.  
## [1] 1 4

2.3 Accessing a column from a matrix

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
d[,1] # If we don't give any row value, then whole row values will be retrived.
## [1] 1 2 3

2.4 Accessing a set of values.

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
d[1:2,1] # 1st row and second row is selected and then the first column is selected.
## [1] 1 2

2.5 Removing elements using negative subscript.

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
d[,-1]
## [1] 4 5 6

3. Changing elements in a matrix.

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
d[1:2,]<-matrix(c(17,18,19,20),nrow = 2,byrow = TRUE)
d
##      [,1] [,2]
## [1,]   17   18
## [2,]   19   20
## [3,]    3    6

4. Filtering a matrix

Retrieving a matrix satisfying a particular condition

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
j<-d[,2]>4 # in 2nd column, find the elements greater than 4
j
## [1] FALSE  TRUE  TRUE
d[j,] # Display the matrix whose second column has greater than 4
##      [,1] [,2]
## [1,]    2    5
## [2,]    3    6

4.1 Using which() function.

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
which(d>3)
## [1] 4 5 6

5. Applying functions to matrix.

Syntax:

apply(m,dimcode,f,fargs)

where the arguments are as follows:

d<-matrix(c(1,17,23,44,45,46), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1   44
## [2,]   17   45
## [3,]   23   46
apply(d,2,mean)
## [1] 13.66667 45.00000

6. Adding and deleting rows and columns of a matrix

6.1 Adding an extra row.

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
e<-c(7,8,9) # Create a vector of elements you want to add as extra column.
d<-cbind(d,e) # Use cbind to combine d and e 

6.2 Adding an extra row of all oneโ€™s

d<-matrix(c(1,2,3,4,5,6), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
rbind(1,d)
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    4
## [3,]    2    5
## [4,]    3    6

6.3 Deleting a row from a matrix

There is no special operation to delete elements from matrix.

d<-matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3) # Consider a matrix d
d
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

Say, we want to remove the second row from a the matrix d

d<-d[c(1,3),]
d
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    3    6    9

6.4 Deleting a column from a matrix.

d<-matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3) # Consider a matrix d
d
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

Say, we want to remove the second column from a the matrix d

d<-d[,c(1,3)]
d
##      [,1] [,2]
## [1,]    1    7
## [2,]    2    8
## [3,]    3    9

7 Some more operations on matrix

7.1 Find the class of a matrix

d<-matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3) # Consider a matrix d
class(d)
## [1] "matrix"

7.2 Getting the dimensions of a matrix

d<-matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3)
dim(d)
## [1] 3 3

7.3 Getting the number of rows and columns of a matrix

d<-matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3)
nrow(d) # Returns the number of rows
## [1] 3
ncol(d) # Returns the number of columns
## [1] 3

7.4 Avoiding Unintended Dimension Reduction.

d<-matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3)
d
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
class(d)
## [1] "matrix"

Now, consider a sub-matrix from the matrix and check its class (data type)

b<-d[1,2:3]
b
## [1] 4 7
class(b)
## [1] "numeric"

In the above case, we get an output which is no more a matrix, but its a vector. But in many cases we may need matrices ( Say an operation to be applied on a sub-matrix)

In order to retain the data-type as matrix we can do the following.

a<-d[1,2:3,drop=FALSE] # Use drop=FALSE to retain the data-type
a
##      [,1] [,2]
## [1,]    4    7
class(a)
## [1] "matrix"

We can convert a vector into a matrix using as.matrix() function.

b<-c(1,2,3,4)
b
## [1] 1 2 3 4
class(b) # Check the class of b
## [1] "numeric"
# Use as.matrix to convert b to a matrix

b<-as.matrix(b) 
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3
## [4,]    4
class(b) # Now check the class
## [1] "matrix"

7.5 Naming Matrix Rows and Columns

We can use colnames() and rownames() to give names to column and rows.

d<-matrix(c(10,15,12,13,15,15), nrow = 3) # Consider a matrix d
d
##      [,1] [,2]
## [1,]   10   13
## [2,]   15   15
## [3,]   12   15
colnames(d)<-c("math","Science")
rownames(d)<-c("Ramesh","Suresh","Mahesh")
d
##        math Science
## Ramesh   10      13
## Suresh   15      15
## Mahesh   12      15

8 Arrays

In R, if there are only 2 dimensions, they are called matrix. But if we have more than 2 dimensions, then are called arrays.

In the above example, say we have marks of 3 students in 2 subjects in MID 1. But say if we want to store MID 2 marks for same set of students for same subjects, we may use arrays.

Note: Here the 3rd dimension is mid exams.

Consider Mid 1 marks

mid1<-matrix(c(10,15,12,13,15,15), nrow = 3) # Consider a matrix d
colnames(mid1)<-c("math","Science")
rownames(mid1)<-c("Ramesh","Suresh","Mahesh")
mid1
##        math Science
## Ramesh   10      13
## Suresh   15      15
## Mahesh   12      15

Mid 2 marks

mid2<-matrix(c(9,12,13,14,12,12), nrow = 3) # Consider a matrix d
colnames(mid2)<-c("math","Science")
rownames(mid2)<-c("Ramesh","Suresh","Mahesh")
mid2
##        math Science
## Ramesh    9      14
## Suresh   12      12
## Mahesh   13      12

Storing of both mid marks in a 3D array.

subject<-c("math","Science")
Students<-c("Ramesh","Suresh","Mahesh")
exam<-c("MID 1","MID 2")
mid<-array(data=c(mid1,mid2),dim = c(3,2,2),dimnames = list(Students,subject,exam))
mid
## , , MID 1
## 
##        math Science
## Ramesh   10      13
## Suresh   15      15
## Mahesh   12      15
## 
## , , MID 2
## 
##        math Science
## Ramesh    9      14
## Suresh   12      12
## Mahesh   13      12

8.1 Accessing elements from an array.

subject<-c("math","Science")
Students<-c("Ramesh","Suresh","Mahesh")
exam<-c("MID 1","MID 2")
mid<-array(data=c(mid1,mid2),dim = c(3,2,2),dimnames = list(Students,subject,exam)) # Consider a matrix
mid
## , , MID 1
## 
##        math Science
## Ramesh   10      13
## Suresh   15      15
## Mahesh   12      15
## 
## , , MID 2
## 
##        math Science
## Ramesh    9      14
## Suresh   12      12
## Mahesh   13      12
mid[1,2,1] # Marks of Ramesh in Science subject 1st mid marks
## [1] 13
mid[,2,1]# Science marks of all students in 1st mid exam
## Ramesh Suresh Mahesh 
##     13     15     15