Open In App

How to find the rank of a matrix in R

Last Updated : 25 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A matrix is an arrangement of data in a row- and column-wise fashion or a matrix is nothing but a set of particular kinds of data like numbers. It has many applications in mathematics, Physics, engineering, etc. The number of rows and columns defines the matrix size called an order. For example, if the matrix has m rows and n columns, the order of the matrix would be m x n. A matrix in R Programming Language is a 2D array that can store numeric, character, or logical data.

R
# Create a matrix of order 2 x 3 in R
my_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)

# Print the matrix
print(my_matrix)

Output:

     [,1]  [,2]  [,3]
[1,]   1     2     3
[2,]  4    5     6

Rank of the Matrix in R Programming Language

The rank of matrix is used in linear algebra to describe the maximum number of linearly independent rows or columns of the matrix. The rank of matrix is written as rank(A) for a matrix A. Rank of matrix is a basic concept of linear algebra which provides certain useful information about properties and behavior of matrix. It is trying to measure up the “dimensionality” or number of pieces in information that are actually independent.

  1. Row Rank & Column Rank: The row rank of a matrix is the maximum number of linearly independent rows. The column rank of a matrix is the maximum number of linearly independent columns.
  2. Rank and Nullity Theorem: According to this theorem, the number of columns in any matrix A is equal to the sum of its rank and nullity (dimension of the null space).
  3. Reduced Row Echelon Form (RREF): The rank of a matrix is the number of non-zero rows in its reduced row echelon form (RREF). It is obtained through row operations that simplify the matrix.
  4. Relation to Solutions of Linear Systems: In a system of linear equations, the rank of a coefficient matrix corresponds to the number of solutions in the system. There is only one solution for the system if the rank is the same as the number of variables. The system has an infinite number of solutions if the rank is smaller than the number of variables.
  5. Linearly Independent: If a set of vectors cannot be described as a linear combination of one another,, then they are termed linearly independent.

For an m x n matrix:

  1. The rank is at most m or n, whichever is smaller.
  2. For a matrix to have full row rank, the rank should be equal to m.
  3. Similarly,, for a full column rank,, the rank should be equal to n.
  4. If rank(A) < min(m, n), the matrix is said to be rank-deficient.

Rank of a matrix is of great significance

  1. The rank of the matrix determines the number of independent equations.
  2. For a matrix to be invertible, it should have full rank.
  3. Rank is quite useful in data analysis and machine learning; it is used to determine the number of independent features or variables.

Methods to find Rank of a Matrix

Using Matrix Package

There is function called rankMatrix which comes under the Matrix Package. This package has a special class for matrices that contains multiple functions for various operations. Now lets work out on some coding part.

  • rankMatrix (matrix): It calculates the rank of the matrix.
  • matrix: It is the input matrix.
R
install.packages("Matrix")
library(Matrix)

# Now we will create a matrix
rank_matrix <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = TRUE)

# Calculate rank using rankMatrix
print(rankMatrix(rank_matrix))

Output:

[1] 2

Using qr function from base R

The qr function of R performs the QR decomposition of a matrix. Q is the orthogonal matrix,, and R is the upper triangular matrix. Here we calculate rank by counting the number of non-zero diagonal elements in the upper triangular matrix obtained from the decomposition.

  • qr (matrix): It does the qr decomposition of the matrix.
  • matrix: It is the input matrix.
R
# Let's first create an example matrix
rank_matrix <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), nrow = 4, byrow = TRUE)

# Now we will calcule rank using qr function
rank_of_matrix <- qr(rank_matrix) $ rank
print(rank_of_matrix)

Output:

[1] 2

The result of the function is a list with two members – component the $qr and rank of the matrix $rank. So using the latter one gives the rank of the matrix.

Using svd (Singular Value Decomposition)

SVD is method of factorization that breaks a matrix into 3 matrices and extracts the singular values. It makes calculating rank very easy which can be calculated by finding the count of non-zero singular values.

  • svd (matrix): It calculates the singular value decomposition of a matrix.
  • matrix: It is the input matrix.
R
# Creating a matrix of order 4 x 4
our_matrix <- matrix(c(10, 11, 12, 13, 14, 5, 6, 7, 8, 9, 1, 2, 3, 4, 15, 16), 
                       nrow = 4, byrow = TRUE)

# Calculate rank using svd
SVD <- svd(our_matrix)$d > 1e-10
rank_of_matrix <- sum(SVD)
print(rank_of_matrix)

Output:

[1] 4

SVD result list consists of 3 elements: $u ’ $d ‘, ‘ $v ‘ which are the left singular vectors, the singular values, and the right singular vectors respectively. Using svd(our_matrix)$d > 1e-10 ,a logical vector is created here, where the check occurs if each singular vector is greater than 1e-10 . The above argument which is placed into the sum() at the end counts the rank.

Using eigenvalues and eigenvectors

Eigenvalues and Eigenvectors are used in the analysis of linear transformations. The rank of the matrix can be determined by counting the number of non-zero eigenvalues.

  • eigen (matrix): It calculates the eigenvalues and eigenvectors of a square matrix.
  • matrix: It is the input matrix.
R
# Create a matrix
our_matrix <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = TRUE)

# Calculate rank using eigen
rank_of_matrix <- sum(abs(eigen(our_matrix)$values) > 1e-10)
print(rank_of_matrix)

Output:

[1] 2

eigen(our_matrix) gives a list containing ‘$values’, ‘$vectors’. eigen(your_matrix)$values filters the values from the list. The argument inside sum function, takes the absolute values of the previous step’s eigenvalues, compares them with the threshold of 1e-10, and forms a logical vector. The sum function calculates the rank by summing up the true value, which is 1 in a logical vector.

Using LinAlg package

The LinAlg package contains a function called the MatrixRank function, which directly calculates the rank of a matrix.

called theSyntax and parametersthe syntax:

  • MatrixRank (matrix): It calculates the rank of the matrix
  • matrix: the input matrix.
R
# Install and load the LinAlg package
install.packages("LinAlg")
library(LinAlg)

# Create a matrix
our_matrix <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = TRUE)

# Calculate rank using LinAlg
rank_of_matrix <- LinAlg::MatrixRank(our_matrix)
print(rank_of_matrix)

Output:

[1] 2


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads