Matrix operations in R (2024)

HOME INTRODUCTION TO R MATRIX OPERATIONS R

Introduction to R Mathematical functions

Matrix operations in R (1)

There are multiple matrix operations that you can perform in R. This include: addition, subtraction and multiplication, calculating the power, the rank, the determinant, the diagonal, the eigenvalues and eigenvectors, the transpose and decomposing the matrix by different methods. In this article we will review how to perform these algebra operations in R.

Addition and subtraction

The most basic matrix operations are addition and subtraction. In the following examples we are going to use the square matrices of the following block of code:

A <- matrix(c(10, 8, 5, 12), ncol = 2, byrow = TRUE)AB <- matrix(c(5, 3, 15, 6), ncol = 2, byrow = TRUE)B
# A # B [, 1] [, 2] [, 1] [, 2][1, ] 10 8 [1, ] 5 3 [2, ] 5 12 [2, ] 15 6

These matrices are both of the same dimensions. You can check the dimensions (number of rows and columns, respectively) of a matrix with the dim function.

dim(A) # 2 2dim(B) # 2 2

On the one hand, with the + operator you can compute an element-wise sum of the two matrices:

A + B
 [, 1] [, 2][1, ] 15 11[2, ] 20 18

On the other hand, the - operator will allow you to substract them:

A - B
 [, 1] [, 2][1, ] 5 5[2, ] -10 6

Transpose a matrix in R

To find the transpose of a matrix in R you just need to use the t function as follows:

t(A)
 [, 1] [, 2][1, ] 10 5[2, ] 8 12
t(B)
 [, 1] [, 2][1, ] 5 15[2, ] 3 6

Matrix multiplication in R

There are different types of matrix multiplications: by a scalar, element-wise multiplication, matricial multiplication, exterior and Kronecker product.

Multiplication by a scalar

In order to multiply or divide a matrix by a scalar you can make use of the * or / operators, respectively:

2 * A
 [, 1] [, 2][1, ] 20 16[2, ] 10 24
A / 2
 [, 1] [, 2][1, ] 5.0 4[2, ] 2.5 6

Element-wise multiplication

The element-wise multiplication of two matrices of the same dimensions can also be computed with the * operator. The output will be a matrix of the same dimensions of the original matrices.

A * B
 [, 1] [, 2][1, ] 50 24[2, ] 75 72

To perform a matricial element-wise multiplication both matrices must be of the same dimensions.

Matrix multiplication

In R, a matricial multiplication can be performed with the %*% operator.

A %*% B
 [, 1] [, 2][1, ] 170 78[2, ] 205 87

Before multiplying two matrices check that the dimensions are compatible. The number of columns of the first matrix must be equal to the number of rows of the second.

Matrix crossproduct

If you need to calculate the matricial product of a matrix and the transpose or other you can type t(A) %*% B or A %*% t(B), being A and B the names of the matrices. However, in R it is more efficient and faster using the crossprod and tcrossprod functions, respectively.

crossprod(A, B)
 [, 1] [, 2][1, ] 125 60[2, ] 220 96
tcrossprod(A, B)
 [,1 ] [, 2][1, ] 74 198[2, ] 61 147

Exterior product

Similarly to the matricial multiplication, in R you can compute the exterior product of two matrices with the %o% operator. This operator is a shortcode for the default outer function.

A %o% B# Equivalent to:outer(A, B, FUN = "*") 
, , 1, 1 [, 1] [, 2][1, ] 50 40[2, ] 25 60, , 2, 1 [, 1] [, 2][1, ] 150 120[2, ] 75 180, , 1, 2 [, 1] [, 2][1, ] 30 24[2, ] 15 36, , 2, 2 [, 1] [, 2][1, ] 60 48[2, ] 30 72

Kronecker product

The Kronecker product of two matrices \(A\) and \(B\), denoted by \(A \otimes B\) is the last type of matricial product we are going to review. In R, the calculation can be achieved with the %x% operator.

A %x% B
 [, 1] [, 2] [, 3] [, 4][1, ] 50 30 40 24[2, ] 150 60 120 48[3, ] 25 15 60 36[4, ] 75 30 180 72

Power of a matrix in R

There is no a built-in function in base R to calculate the power of a matrix, so we will provide two different alternatives.

On the one hand, you can make use of the %^% operator of the expm package as follows:

# install.packages("expm")library(expm) A %^% 2
 [, 1] [, 2][1, ] 140 176[2, ] 110 184

On the other hand the matrixcalc package provides the matrix.power function:

# install.packages("matrixcalc")library(matrixcalc) matrix.power(A, 2)
 [, 1] [, 2][1, ] 140 176[2, ] 110 184

You can check that the power is correct with the following code:

A %*% A

The matrix must be square to calculate the power, as the number of columns must be equal to the number of rows to compute the calculations.

Note that if you want to calculate the element-wise power you just need to use the ^ operator. In this case the matrix don’t need to be square.

A ^ 2
 [, 1] [, 2][1, ] 100 64[2, ] 25 144

Determinant of a matrix in R

The determinant of a matrix \(A\), generally denoted by \(|A|\), is a scalar value that encodes some properties of the matrix. In R you can make use of the det function to calculate it.

det(A) # 80det(B) # -15

Inverse of a matrix in R

In order to calculate the inverse of a matrix in R you can make use of the solve function.

M <- solve(A)M
 [, 1] [, 2][1, ] 0.1500 -0.100[2, ] -0.0625 0.125

As a matrix multiplied by its inverse is the identity matrix we can verify that the previous output is correct as follows:

A %*% M
 [, 1] [, 2][1, ] 1 0[2, ] 0 1

Moreover, as main use of the solve function is to solve a system of equations, if you want to calculate the solution to \(A\) %*% \(X = B\) you can type:

solve(A, B)
 [, 1] [, 2][1, ] -0.7500 -0.1500[2, ] 1.5625 0.5625

Rank of a matrix in R

The rank of a matrix is maximum number of columns (rows) that are linearly independent. In R there is no base function to calculate the rank of a matrix but we can make use of the qr function, which in addition to calculating the QR decomposition, returns the rank of the input matrix. An alternative is to use the rankMatrix function from the Matrix package.

qr(A)$rank # 2qr(B)$rank # 2# Equivalent to:library(Matrix)rankMatrix(A)[1] # 2

Matrix diagonal in R

The diag function allows you to extract or replace the diagonal of a matrix:

# Extract the diagonaldiag(A) # 10 12 diag(B) # 5 6# Replace the diagonal# diag(A) <- c(0, 2)

Applying the rev function to the columns of the matrix you can also extract off the elements of the secondary diagonal matrix in R:

# Extract the secondary diagonalsdiag(apply(A, 2, rev)) # 5 8diag(apply(B, 2, rev)) # 15 3

Diagonal matrix

With the diag function you can also make a diagonal matrix, passing a vector as input of the function.

diag(c(7, 9, 2))
 [, 1] [, 2] [, 3][1, ] 7 0 0[2, ] 0 9 0[3, ] 0 0 2

Identity matrix in R

In addition to the previous functionalities, the diag function also allows creating identity matrices, specifying the dimension of the desired matrix.

diag(4)
 [, 1] [, 2] [, 3] [, 4][1, ] 1 0 0 0[2, ] 0 1 0 0[3, ] 0 0 1 0[4, ] 0 0 0 1

Eigenvalues and eigenvectors in R

Both the eigenvalues and eigenvectors of a matrix can be calculated in R with the eigen function.

On the one hand, the eigenvalues are stored on the values element of the returned list. The eigenvalues will be shown in decreasing order:

eigen(A)$values # 17.403124 4.596876eigen(B)$values # 12.226812 -1.226812

On the other hand, the eigenvectors are stored on the vectors element:

eigen(A)$vectors
 [, 1] [, 2][1, ] -0.7339565 -0.8286986[2, ] -0.6791964 0.5596952
eigen(B)$vectors
 [, 1] [, 2][1, ] -0.3833985 -0.4340394[2, ] -0.9235830 0.9008939

Singular, QR and Cholesky decomposition in R

In this final section we are going to discuss how to perform some decompositions related with matrices.

First, the Singular Value Decomposition (SVD) can be calculated with the svd function.

svd(A)
$d[1] 17.678275 4.525328$u [, 1] [, 2][1, ] -0.7010275 -0.7131342[2, ] -0.7131342 0.7010275$v [, 1] [, 2][1, ] -0.5982454 -0.8013130[2, ] -0.8013130 0.5982454

The function will return a list, where the element d is a vector containing the singular values sorted in decreasing order and u and v are matrices containing the left and right singular vectors of the original matrix, respectively.

Second, the qr function allows you to calculate the QR decomposition. The first element of the output will return a matrix of the same dimension as the original matrix, where the upper triangle matrix contains the \(\bold{R}\) of the decomposition and the lower the \(\bold{Q}\).

qr(A)$qr
 [, 1] [, 2][1, ] -11.1803399 -12.521981[2, ] 0.4472136 7.155418

Last, you can compute the Cholesky factorization of a real symmetric positive-definite square matrix with the chol function.

chol(A)
 [, 1] [, 2][1, ] 3.162278 2.529822[2, ] 0.000000 2.366432

The chol function doesn’t check for symmetry. However, you can make use of the isSymmetric function to check it.

Matrix operations in R (2024)

FAQs

What are the operations with matrix in R? ›

Matrix Math Operations Using R
  • Inner product.
  • Outer product.
  • Kronecker product.
  • Diagonals.
  • Eigenvalues and Eigenvectors.
  • Singular Value Decomposition.
  • Cholesky decomposition.
  • QR decomposition.
Sep 1, 2022

What does matrix () do in R? ›

Matrices are created using the matrix() function, which takes the data elements and dimensions as input parameters. The elements can be numeric, character, logical, or any other data type supported by R.

How to know what row operations to do? ›

The three matrix row operations are: switching, addition, and multiplication. Switching switches two rows. Addition adds two rows and replaces one of the rows with the sum. Multiplication multiplies all of the numbers in the row by one number.

How do you create a matrix in R? ›

To create a matrix in R you need to use the function called matrix(). The arguments to this matrix() are the set of elements in the vector. You have to pass how many numbers of rows and how many numbers of columns you want to have in your matrix. Note: By default, matrices are in column-wise order.

How do you use matrix operations? ›

To add two matrices, we add the numbers of each matrix that are in the same element position. We can subtract matrices in a similar way to addition. Both matrices need to have the same dimensions, and we subtract the numbers of the second matrix from the first that are in the same element position.

How many matrix operations are there? ›

The matrix operations include the addition, subtraction, multiplication of matrices, transpose of a matrix, and inverse of a matrix. The addition, subtraction, multiplication of matrices include two or more matrices, and the transpose, inverse operations is performed on only one matrix.

What are the 5 matrix rules? ›

Laws
(1) Commutative Law of AdditionA+B=B+A
(2) Associative Law of AdditionA+(B+C)=(A+B)+C
(3) Distributive Law of a Scalar over Matricesc(A+B)=cA+cB, where c∈R.
(4) Distributive Law of Scalars over a Matrix(c1+c2)A=c1A+c2A, where c1,c2∈R.
(5) Associative Law of Scalar Multiplicationc1(c2A)=(c1⋅c2)A, where c1,c2∈R.
10 more rows
Aug 16, 2021

What are the three rules of row operation? ›

The three elementary row operations are: (Row Swap) Exchange any two rows. (Scalar Multiplication) Multiply any row by a constant. (Row Sum) Add a multiple of one row to another row.

What is the difference between row operations and column operations in a matrix? ›

We know that elementary row operations are the operations that are performed on rows of a matrix. Similarly, elementary column operations are the operations that are performed on columns of a matrix. The three basic elementary operations or transformation of a matrix are: Interchange of any two rows or two columns.

How to manipulate matrices in R? ›

The cbind and rbind functions are used to append matrices together. The dimnames function is used to manipulate the row and column names of a matrix. By using the bracket notation it is possible to access the rows, columns or elements in the matrix.

What is R matrix theory? ›

Initially the R-matrix theory was aimed at describing resonances in nuclear reactions. At present, the main aim of the R-matrix theory is to describe the scattering and reactions resulting from the interaction of particles or systems of particles, which can be nucleons, nuclei, electrons, atoms, or molecules.

How to convert data into matrix in R? ›

Convert a Data Frame into a Numeric Matrix in R Programming – data. matrix() Function. data. matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix.

What is the structure of a matrix in R? ›

In R matrices are two-dimensional collections of elements all of which have the same mode or type. This is different than a data frame in which the columns of the frame can hold elements of different type (but all of the same length), or from a list which can hold objects of arbitrary type and length.

What is R matrix method? ›

Initially the R-matrix theory was aimed at describing resonances in nuclear reactions. At present, the main aim of the R-matrix theory is to describe the scattering and reactions resulting from the interaction of particles or systems of particles, which can be nucleons, nuclei, electrons, atoms, or molecules.

How to solve matrices in R? ›

For a 5 × 5 identity matrix,
  1. diag(5) ...
  2. # Grab the diagonal elements of the matrix C diag(C) ...
  3. # Take the inverse of C solve(C) ...
  4. # Multiply C by its inverse C %*% solve(C) ...
  5. # The determinant of a matrix det(C) ...
  6. # The trace of a matrix using the psych package's tr() library(psych) tr(C) ...
  7. sum(diag(C))

Top Articles
Enduring Word Bible Commentary Isaiah Chapter 28
Isaiah 28 Chapter Summary
Northern Counties Soccer Association Nj
NYT Mini Crossword today: puzzle answers for Tuesday, September 17 | Digital Trends
Txtvrfy Sheridan Wy
Osrs But Damage
Deshret's Spirit
Geometry Escape Challenge A Answer Key
OnTrigger Enter, Exit ...
Daniela Antury Telegram
Delectable Birthday Dyes
Helloid Worthington Login
Calmspirits Clapper
Leader Times Obituaries Liberal Ks
St Maries Idaho Craigslist
Plan Z - Nazi Shipbuilding Plans
Healthier Homes | Coronavirus Protocol | Stanley Steemer - Stanley Steemer | The Steem Team
Tyrone Unblocked Games Bitlife
Rs3 Ushabti
Silky Jet Water Flosser
Craigslist Wilkes Barre Pa Pets
Used Patio Furniture - Craigslist
Victory for Belron® company Carglass® Germany and ATU as European Court of Justice defends a fair and level playing field in the automotive aftermarket
Cognitive Science Cornell
Malluvilla In Malayalam Movies Download
Lcsc Skyward
R/Mp5
Stubhub Elton John Dodger Stadium
Plasma Donation Racine Wi
DIY Building Plans for a Picnic Table
Pfcu Chestnut Street
Flaky Fish Meat Rdr2
Adecco Check Stubs
Tributes flow for Soundgarden singer Chris Cornell as cause of death revealed
Netherforged Lavaproof Boots
Justin Mckenzie Phillip Bryant
Junior / medior handhaver openbare ruimte (BOA) - Gemeente Leiden
Blue Beetle Movie Tickets and Showtimes Near Me | Regal
Vivek Flowers Chantilly
Stafford Rotoworld
2700 Yen To Usd
Joey Gentile Lpsg
Let's co-sleep on it: How I became the mom I swore I'd never be
Download Diablo 2 From Blizzard
Lovely Nails Prices (2024) – Salon Rates
Conan Exiles Armor Flexibility Kit
Anderson Tribute Center Hood River
Mikayla Campinos Alive Or Dead
Joe Bartosik Ms
Otter Bustr
Ubg98.Github.io Unblocked
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 5329

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.