Cheat Sheet of Matrix Operations in R

If you spend some time doing data analysis, sooner or later you will need to get your hands on matrix manipulations, so here’s a cheat sheet of R functions for matrix operations and manipulations.

First let’s create three matrices that we are going to play with

# generate a rectangular matrix with 10 rows and 3 columns
set.seed(777)
A = matrix(runif(30), nrow=10, ncol=3)

# generate a rectangular matrix with 3 rows and 5 columns
set.seed(888)
B = matrix(runif(15), nrow=3, ncol=5)

# generate a square matrix with 4 rows and 4 columns
set.seed(999)
C = matrix(runif(16), nrow=4, ncol=4)

Inspection of matrices

# dimensions of matrix A
dim(A)

# the same results can be obtained with nrow and ncol
nrow(A)
ncol(A)

# assign row and column names to matrix A
rownames(A) = 1:10
colnames(A) = paste("a", 1:3, sep="")

# similarly with matrix B and C
dimnames(B) = list(1:3, paste("b", 1:5, sep=""))
dimnames(C) = list(1:4, paste("c", 1:4, sep=""))

# what class of object is A?
class(A)

# what is the type of storage in A?
typeof(A)

# how much space is allocated to A?
object.size(A)

# show first 4 rows of matrix A
head(A, 4)

# show last 4 rows of matrix A
tail(A, 4)

# descriptive statistics of variables in A
summary(A)

Some matrix operations

# matrix multiplication AB
A %*% B

# transpose of B
t(B)

# matrix product B'A'
t(B) %*% t(A)

# scalar multiplication
5 * B
B * 5

# extract elements in the diagonal of a square matrix
diag(C)

# trace of a square matrix
sum(diag(C))

# determinant of a square matrix
det(C)

# create an identity matrix of 5x5
Iden = diag(1, 5)
Iden

# inverse of square matrix
solve(C)

# singular value decomposition
svd(A)

# eigen-decomposition of a symmetric matrix
eigen(C %*% t(C))

Functions for basic calculations

# sum of elements by rows
rowSums(A)

# sum of elements by columns
colSums(A)

# another way to get sum of elements by rows
margin.table(A, 1)

# another way to get sum of elements by columns
margin.table(A, 2)

# mean of elements by rows
rowMeans(A)

# mean of elements by columns
colMeans(A)

Function apply

# sum of elements by rows
apply(A, 1, sum)

# sum of elements by columns
apply(A, 2, sum)

# mean of elements by rows
apply(A, 1, mean)

# means of elements by columns
apply(A, 2, mean)

# maximum by rows
apply(A, 1, max)

# minimum by columns
apply(A, 2, min)

# which position is the minimum by row
# (with your own function)
myfun <- function(x) {which(x == min(x))}
apply(A, 1, myfun)

Some extra handy functions

# center matrix A
scale(A, scale=FALSE)

# alternatively
sweep(A, 2, colMeans(A))

# standardize matrix A (variables with mean=0 and var=1)
scale(A)

# elements as fraction of the total sum
prop.table(A)

# elements as fraction of rows margin
prop.table(A, 1)

# elements as fraction of columns margin
prop.table(A, 2)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s