In this tutorial you will explore matrix in R.You will learn to create a matrix, there are different methods in creating matrices you can learn and choose the most appropriate method for creating a matrix of your choice. Further naming rows and columns in a matrix, checking matrix existence, selecting, modifying, and deleting elements from a matrix.
A matrix is like a sibling of the vector. You know a vector is a data structure with a sequence of data elements that is dimensional and a matrix is also a similar collection of data elements but the difference is elements are arranged into a fixed number of rows and columns. Since matrix data elements are arranged in rows and columns they are called two-dimensional.
Vector -> 1D | Matrix -> 2D |
---|---|
To create a vector > |
To create a matrix > |
Output [1] 3 5 4 8 |
Output [,1] [,2] |
![]() |
![]() |
Note: Don’t bother about the syntax just understand the concept.
A matrix is a two-dimensional array (2D) with rows and columns where the matrix contains elements of similar basic data types like numerals, character, logical, etc.
The following is an example of a matrix with 2 rows and 2 columns.
In R, a matrix is created using matrix()
function.
The basic Syntax 1 to create a matrix is a matrix()
function with arguments such as data, number of rows, number of columns passed inside the parentheses.
matrix(data,nrow,ncol) #Syntax 1
Where the arguments inside matrix()
are
> MATRIX <- matrix(1:15, nrow=3, ncol=5)
> MATRIX
Output:
[,1] [,2] [,3] [,4] [,5] [1,] 1 4 7 10 13 [2,] 2 5 8 11 14 [3,] 3 6 9 12 15
matrix(data, nrow, ncol, byrow =TRUE) #Syntax 2
Where byrow =TRUE distribute the elements row-wise.
> MATRIX <- matrix(1:15, nrow=3, ncol=5,byrow =TRUE)
> MATRIX
Output:
[,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 6 7 8 9 10 [3,] 11 12 13 14 15
Syntax 1 arranges the data in column-wise by default, in syntax 2 we passed the argument byrow = TRUE
which arranges the same data inside the matrix row-wise.
The numbers 1 2 3 4 5 are arranged column-wise in Syntax 1 where the same data(numbers) is arranged as rows by using byrow =TRUE
while creating a matrix in Syntax 2.
Note: By default, matrices are created with column-wise data in that case byrow=FALSE.You can either give byrow=FALSE else by default it takes column-wise arrangement of data.
In R language they provide a built-in function rbind()
to create a matrix by filling data or elements row-wise.In R programming rbind stands for row binding.
rbind(data)
Example: matrix<- rbind(Item_NO,Item_name)
// Place code hereItem_NO <- c(1:4)
Item_name<-c("MILK","CHEESE","BUTTER","CURD")
matrix<- rbind(Item_NO,Item_name)
print(matrix)
Output produced by the above code
NO "1" "2" "3" "4" Item_name "MILK" "CHEESE" "BUTTER" "CURD"
You can infer from the output that the set of data are distributed along the rows.
In R language they provide a built-in function cbind()
to create a matrix by filling data or elements column-wise.R programming cbind stands for column binding.
cbind(data)
Example : matrix<- cbind(Item_NO,Item_name)
Let us see a code
Item_NO <- c(1:4)
Item_name<-c("MILK","CHEESE","BUTTER","CURD")
matrix<- cbind(Item_NO,Item_name)
print(matrix)
Output:
Item_NO Item_name [1,] "1" "MILK" [2,] "2" "CHEESE" [3,] "3" "BUTTER" [4,] "4" "CURD"
You can infer from the output the data is displayed column-wise with Item_NO corresponding to each Item_name column-wise.
Note in both rbind()
and cbind()
we can have deparse.level
. The deparse.level
can be set to 0,1 which constructs labels to the matrix.
Consider the table below to better infer the output of rbind()
in different values for deparse.level
.
Function | Code | Output |
---|---|---|
rbind() |
matrix<- rbind(Item_NO,Item_name,deparse.level = 0)
matrix<- rbind(Item_NO,tem_name,deparse.level = 1) |
[,1] [,2] [,3] [,4] [,1] [,2] [,3] [,4] |
cbind() |
matrix<- cbind(Item_NO, item_name,deparse.level = 0)
matrix<- cbind(Item_NO,Item_name, deparse.level = 1) |
[,1] [,2] Item_NO Item_name
|
NOTE : Both cbind() and rbind() are built-in functions in R to create matrices by combining several vectors of the same length.
The deparse.level values 0 or 1 determine the labels to construct (column labels for cbind or row labels for rbind).
Matrices refer to the standards used while creating a Matrix. Using the syntax for creating a matrix, a matrix m is created.
m <- matrix(1:15,
nrow=5,
ncol=3,
byrow=FALSE)
The following are the metrics of a matrix
The matrix m is
[,1] [,2] [,3] [1,] 1 6 11 [2,] 2 7 12 [3,] 3 8 13 [4,] 4 9 14 [5,] 5 10 15
Examples of matrix functions are shown below
nrow(m)
ncol(m)
dim(m)
It produces an output
> print(nrow(m)) [1] 5 > print(ncol(m)) [1] 3 > print(dim(m)) [1] 5 3
Consider the above-created MATRIX using the matrix()
function, let us give names to the rows and columns of MATRIX using the following syntax
To name rows of the matrix
rownames(<MATRIX>) = c(<name1>,<name2>………)
Example: rownames(MATRIX) =c("row_1","row_2","row_3")
To name coloumn of the matrix
colnames(<MATRIX>)=c(<name1>,<name2>…….)
Example: colnames(MATRIX)=c("col_1","col_2","col_3","col_4","col_5")
Let us see a simple program to understand the naming of matrix rows and columns
#created a matrix named MATRIX
#MATRIX is 3 by 5 with data distributed row wise
MATRIX <- matrix(1:15, nrow=3, ncol=5,byrow =TRUE)
#create names to rows
rownames(MATRIX)=c("row_1","row_2","row_3")
#create names to columns
colnames(MATRIX)=c("col_1","col_2","col_3","col_4","col_5")
cat("The MATRIX after naming is \n")
print(MATRIX)
Output:
The MATRIX after naming is col_1 col_2 col_3 col_4 col_5 row_1 1 2 3 4 5 row_2 6 7 8 9 10 row_3 11 12 13 14 15
Let us see how these code looks in RStudio
In R you can name matrix rows and columns using dimnames() function. It is a built in function in R to set values to row and columns as well as to get names of rows and columns.
dimnames() <- list(c(), c())
Start by creating a matrix using matrix() function
m <- matrix(1:15,
nrow=5,
ncol=3,
byrow=FALSE)