Working with Vectors and Matrices in R

This session is intended to introduce some of the features in R for dealing with vectors and matrices. We will be using these features throughout the semester, so learn this stuff well.

Vectors

R handles vector objects quite easily and intuitively.

x<-c(1,3,2,10,5)    #create a vector x with 5 components

x

y<-1:5              #create a vector of consecutive integers

y

2*y                 #scalar multiplication

y^2                 #raise each component to the second power

2^y                 #raise 2 to the first through fifth power

y                   #y itself has not been unchanged

y<-y*2

y                   #it is now changed

More examples of vector arithmetic:

x<-c(1,3,2,10,5); y<-1:5 #two or more statements are separated by semicolons

x;y

x+y

x*y

x/y

x^y

sum(x)            #sum of elements in x

cumsum(x)         #cumulative sum vector

diff(x)           # first difference

diff(x,2)         #second difference

max(x)            #maximum

min(x)            #minimum

Sorting can be done using sort() command:

x

sort(x)                # increasing order

sort(x, decreasing=T)  # decreasing order

Component extraction is a very important part of vector calculation.

x

length(x)           # number of elements in x

x[3]                # the third element of x

x[3:5]              # the third to fifth element of x, inclusive

x[-2]               # all except the second element

x[x>3]              # list of elements in x greater than 3

Logical vectors can be handy:

x>3

as.numeric(x>3)     # as.numeric() function coerces logical components to numeric

sum(x>3)            # number of elements in x greater than 3

(1:length(x))[x<=2] # indices of x whose components are less than or equal to 2

z<-as.logical(c(1,0,0,1)) # numeric to logical vector conversion

z

Character vector:

colors<-c("green", "blue", "orange", "yellow", "red")

colors

Individual components can be named and referenced by their names.

names(x)            # check if any names are attached to x

names(x)<-colors    # assign the names using the character vector colors

names(x)

x

x["green"]          # component reference by its name

names(x)<-NULL      # names can be removed by assigning NULL

x

seq() and rep() provide convenient ways to a construct vectors with a certain pattern.

seq(10)

seq(0,1,length=10)

seq(0,1,by=0.1)

rep(1,3)

c(rep(1,3),rep(2,2),rep(-1,4))

rep("Small",3)

c(rep("Small",3),rep("Medium",4))

rep(c("Low","High"),3)

Matrices

A matrix refers to a numeric array of rows and columns. One of the easiest ways to create a matrix is to combine vectors of equal length using cbind(), meaning "column bind":

x

y

m1<-cbind(x,y);m1

t(m1)                # transpose of m1

m1<-t(cbind(x,y))    # Or you can combine them and assign in one step

m1

dim(m1)              # 2 by 5 matrix

m1<-rbind(x,y)       # rbind() is for row bind and equivalent to t(cbind()).

m1

Of course you can directly list the elements and specify the matrix:

m2<-matrix(c(1,3,2,5,-1,2,2,3,9),nrow=3);m2

Note that the elements are used to fill the first column, then the second column and so on. To fill row-wise, we specify byrow=T option:

m2<-matrix(c(1,3,2,5,-1,2,2,3,9),ncol=3,byrow=T);m2

Extracting the components of a matrix involves one or two indices.

m2

m2[2,3]            #element of m2 at the second row, third column

m2[2,]             #second row

m2[,3]             #third column

m2[-1,]            #submatrix of m2 without the first row

m2[,-1]            #ditto, sans the first column

m2[-1,-1]          #submatrix of m2 with the first row and column removed

Matrix computation is usually done component-wise.

m1<-matrix(1:4, ncol=2); m2<-matrix(c(10,20,30,40),ncol=2)

m1; m2

2*m1                # scalar multiplication

m1+m2               # matrix addition

m1*m2               # component-wise multiplication

Note that m1*m2 is NOT the usual matrix multiplication. To do the matrix multiplication, you should use %*% operator instead.

m1 %*% m2

solve(m1)            #inverse matrix of m1

solve(m1)%*%m1       #check if it is the inverse

diag(3)              #diag() is used to construct a k by k identity matrix

diag(c(2,3,3))       #or other diagonal matrices

Rank of a matrix. There is no rank command, as in some packages.

However, you can get the rank of a matrix using the qr factorization

qr(m1)\$rank   # Returns the matrix rank.

det(m1); det(m2)  #Calculate the determinant of a matrix using det()

eigen(m2) #Eigenvalues and eigenvectors of a matrix is handled by eigen() function:

Matrix Factorization

m <- matrix(c(5,1,1,3),2,2)  # Create a matrix known to be symmetrical and positive definite

m

cm <- chol(m)   # Compute the choleski factorization of m.

cm

Check the choleski factorization of m, i.e,. cm'%*%cm=m and cm%*%cm=m

t(cm) %*% cm  #-- = 'm'

crossprod(cm)  #-- = 'm'

The Singular Value Decomposition of a rectangular matrix X is computed by

X = U D V',

where U and V are orthogonal, V' means V transposed, and D is a diagonal matrix with the singular values D[i,i].

Equivalently, D = U' X V. Note that this is useful for diagonalizing a matrix.

hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, "+") } # Create a Hilbert matrix for fun.

X <- hilbert(9)[,1:6]

s <- svd(X)

s

Check the factorization

D <- diag(s\$d)

s\$u %*% D %*% t(s\$v) #  X = U D V'

t(s\$u) %*% X %*% s\$v #  D = U' X V

D