R offers extensive matrix handling capabilities. In addition to the basic operations discussed earlier, there are several advanced matrix functions that will ease your statistical programming efforts. This tutorial will illustrate such functions with examples.
Table of Contents
Transpose of Matrix in R
The transpose of a matrix A is simply another matrix with the rows and columns interchanged. This can be calculated using t(A)
in R.
#Define a rectangular matrix > rectmatrix<-matrix(c(2,3,4,5,6,7),nrow=2,ncol=3) > rectmatrix [,1] [,2] [,3] [1,] 2 4 6 [2,] 3 5 7 > rectmatrix [,1] [,2] [,3] [1,] 2 4 6 [2,] 3 5 7 > t(rectmatrix) [,1] [,2] [1,] 2 3 [2,] 4 5 [3,] 6 7

Diagonals of a Matrix in R
R has the diag()
function to create a diagonal matrix from a vector. The same function can also be used to retrieve the elements along the principal diagonal of a matrix. Even for non-square matrices, the diag()
function returns the diagonal starting from elements [1,1], [2,2] …etc till no such elements can be retrieved.
#diag(x) where x is a scalar - returns an identity matrix of dimension x by #x > diagmatrix<-diag(4) > diagmatrix [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 1 0 0 [3,] 0 0 1 0 [4,] 0 0 0 1 #diag(x,y,z) - returns a matrix of size y by z with element x along its #diagonal > diagmatrix1<-diag(4,2,2) > diagmatrix1 [,1] [,2] [1,] 4 0 [2,] 0 4 #Define a rectangular matrix > rectmatrix<-matrix(c(2,3,4,5,6,7),nrow=2,ncol=3) > rectmatrix [,1] [,2] [,3] [1,] 2 4 6 [2,] 3 5 7 #Extract its diagonal - returns [1,1] and [2,2] elements of rectmatrix > diag(rectmatrix) [1] 2 5
Cross product and Outer product Matrix Operations in R
We have already seen element-wise multiplication and matrix multiplication earlier. Matrices also have two other kinds of products that are supported by R.
- Outer product: In the simplest terms, the outer product is defined over two vectors v1 and v2, resulting in a matrix that consists of every element of v1 multiplied by every element of v2. If v1 is of length m and v2 is of length n, the outer product is a matrix of dimension m by n. This is also known as the tensor product sometimes. This notion can also be generalized to matrices and known as the Kroeckner product in that case. The R operator for this is
%o%
. - Cross product: The result of a cross product for two vectors A and B is another vector C that is orthogonal to both A and B. Though the intuition behind this is not obviously evident, cross-product has several applications in mathematics, physics, and statistics. This can be done using the
crossprod()
function in R.
Let us look at examples to illustrate these.
#Calculate the vector outerproducts using R > vec1 <- c(2,4,6) > vec2 <-c(5,2,2,2,8) > op <- vec1%o%vec2 > op [,1] [,2] [,3] [,4] [,5] [1,] 10 4 4 4 16 [2,] 20 8 8 8 32 [3,] 30 12 12 12 48 #Get the crossproduct of a matrix with itself > x [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 [3,] 5 7 11 > y [,1] [,2] [,3] [1,] 3 7 1 [2,] 2 8 54 [3,] 6 22 10 > crossprod(x) [,1] [,2] [,3] [1,] 50 65 115 [2,] 65 89 147 [3,] 115 147 266 #Crossproduct of two vectors x and y > crossprod(x,y) [,1] [,2] [,3] [1,] 47 163 269 [2,] 60 216 396 [3,] 108 370 604
Column and Row Matrix Operations in R
R provides several handy functions for combining matrices and generating sums and means of rows and columns. These functions are listed below with examples.
cbind()
: Combines two or more matrices or data frames column-wise to return a new matrix/data frame.
> x [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 [3,] 5 7 11 > y [,1] [,2] [,3] [1,] 3 7 1 [2,] 2 8 54 [3,] 6 22 10 > cbind(x,y) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 3 2 8 3 7 1 [2,] 4 6 9 2 8 54 [3,] 5 7 11 6 22 10 >
rbind()
: Combines two more matrices or data frames row-wise similar to the above function.
> x [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 [3,] 5 7 11 > y [,1] [,2] [,3] [1,] 3 7 1 [2,] 2 8 54 [3,] 6 22 10 > rbind(x,y) [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 [3,] 5 7 11 [4,] 3 7 1 [5,] 2 8 54 [6,] 6 22 10
colSums()
and colMeans()
: Generates sums and means of elements column-wise for a matrix or data frame.
> x [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 [3,] 5 7 11 > colSums(x) [1] 12 15 28 > colMeans(x) [1] 4.000000 5.000000 9.333333
rowSums()
and rowMeans()
: Generates sums and means of elements row-wise for a matrix or data frame.
#Define x and y matrices > x<-matrix(c(3,4,5,2,6,7,8,9,11),ncol=3,nrow=3) > y<-matrix(c(3,2,6,7,8,22,1,54,10),ncol=3,nrow=3) > x [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 [3,] 5 7 11 > rowSums(x) [1] 13 19 23 > rowMeans(x) [1] 4.333333 6.333333 7.666667
References:
- Wikipedia – Cross Product
- Wikipedia – Outer product
- StackExchange – Outerproduct of two matrices
- StackOverflow – What is R’s crossproduct function?