Linear algebra is the math of vectors and matrices.

Thus, this article has two aims:

1) Introducing linear algebra (vectors and matrices) and

2) showing how to work with these concepts in R

## Concepts

#### Scalar

A scalar is just a single value, an integer or a real number. E.g. 5

#### Vectors

A vector is a one-dimensional array of n elements.

1 |
v = (1,4,6,7) |

Elements are indexed by location in the vector. Therefore, x[i] is the *i-th* element: x[2] = 4

A vector can also be interpreted as a matrix that consists of only one row or column.

- Matrices that consist of a single column are sometimes called
**column vectors**.

- Matrices that consist of a single row are sometimes referred to as
**row vectors**.

This will be important when multiplying vectors with matrices.

#### Matrices

A matrix is a two dimensional array of m vectors, each with n elements.

But rather than saying that a matrix has m rows and n columns, mathematicians tend to say that the matrix is of **order** m × n.

For instance, the following is a matrix of order 3 x 2:

Elements are indexed by location in the matrix. From the example above:

a[i,j] is the element in the *i-th* row and *j-th* column. E.g. a[2,2] = 4

*TIP: Usually, vectors are bold-faced lower case letters and matrices are capitalized. *

## Creating vectors

R operates on named *data structures*.

“Coincidentally”, the simplest such structure is the numeric vector.

*Tip: Even a scalar occurring by itself in an expression is taken as a vector of length one.*

To set up a vector named **v**, consisting of four numbers, namely 1, 4, 6, 7, we use the R command:

1 |
x <- c(1,4,6,7) |

This is an assignment statement using the function c() which concatenates its arguments end to end.

1 |
[1] 1 4 6 7 |

Assignment can also be made using the function assign().

1 |
assign("x", c(1,4,6,7)) |

Or can also be made in the other direction, using the obvious change in the assignment operator:

1 |
c(1,4,6,7) -> x |

## Creating Matrices

The two key procedures for creating *data structures* in R are to enter them by hand or to read the elements from an external file.

### Entering Matrices by Hand

The syntax for creating a matrix is:

1 |
matrixName<-matrix(#, nrow=m, ncol=n, byrow=T) |

Where **matrixName** is the selected name of the matrix,** #** are the values, **m** is the number of rows, and** n** is the number of columns. The option **byrow** = T (T stands for true) forces R to read the inputs row-by-row, as opposed to column-by-column (which is how it organizes data internally). For instance,

1 |
A<-matrix(0, nrow=3, ncol=2, byrow=T) |

creates a 3×2 matrix A with zeroes as its elements.

1 2 3 4 |
[,1] [,2] [1,] 0 0 [2,] 0 0 [3,] 0 0 |

If we want to make clear explicitly that the matrix does not yet contain real values, then we can use **NA** for #, which is R’s internal code for missing values.

If the elements of the matrix are different values:

1 |
matrixName<-matrix(c(...),nrow=m,ncol=n,byrow=T) |

For instance,

1 |
A<-matrix(c(1,2,3,4,5,6),nrow=3,ncol=2,byrow=T) |

creates the following matrix:

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

As was said before, R read the matrix, by default, column by column. If we leave out the **byrow** option, then R would create the following matrix:

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

### Creating Matrices from Data

Previous considerations about the files for this example:

- The data reside in a
**tab**or**space**separated ASCII file and missing values are not represented by blanks. Comma separated is not a valid format. - You have put your file under the “Initial working directory” or you know the full path where your file is located.
*Tip: R uses Unix syntax for the filename*(/)*.* - You can copy/paste the data:

1 2 3 4 5 6 7 8 9 |
Filename: AptSize.csv 90 75 92 75 90 53 83 78 85 52 105 90 136 78 |

The syntax to create a matrix from a data file is:

1 |
matrixName<-matrix(scan(filename),nrow=m,ncol=n,byrow=T) |

If the data are organized in the typical format, with columns indicating variables and rows indicating units, then the byrow = T option should be included.

Let’s run the following example:

1 |
B<-matrix(scan("AptSize.csv"),nrow=7,ncol=2,byrow=T) |

R will read the data from the file and store them into the 7 × 2 matrix B.

1 2 3 4 5 6 7 8 |
[,1] [,2] [1,] 90 75 [2,] 92 75 [3,] 90 53 [4,] 83 78 [5,] 85 52 [6,] 105 90 [7,] 136 78 |

*Tips: *

*To obtain a list of all the matrices: ls()*

*To show the contents of a matrix, type the name of the matrix. For instance, A*

## Matrix Types

### Square Matrices

If a matrix has as many columns as rows—i.e., the matrix is of order m × m—it is called a

squarematrix.

Matrices that are not **square** are generally referred to as **rectangular**.

For instance:

1 2 3 4 5 6 |
B<-matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3,byrow=T) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 |

### Symmetric Matrices

Matrices are symmetric when a[i,j] = a[j,i].

Let’s see an example:

1 2 3 4 5 6 |
B<-matrix(c(1,2,3,2,4,1,3,1,2),nrow=3,ncol=3,byrow=T) [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 4 1 [3,] 3 1 2 |

If we “trace” a diagonal (values 1,4,2), we can observe that the rest of the values are “mirrored” at the diagonal.

[,1] [,2] [,3]

[1,] 1 2 3

[2,] 2 4 1

[3,] 3 1 2

### Diagonal Matrices

Diagonal matrices are a sub-set of symmetric matrices. A

diagonal matrixis a square matrix with 0 on all off–diagonal entries and elements d1,d2,…,dn on the diagonal.

It is often written: *diag{d1, d2, . . . , dn}*

1 2 3 4 5 6 |
diag(c(1,2,3)) [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 2 0 [3,] 0 0 3 |

#### Scalar Matrices

Scalar matrices are a special case of diagonal matrices: all the diagonal elements are equal to the same scalar k: a[i,i] = k for all i.

For instance, let’s create a 3 × 3 scalar matrix with k = 7.

1 2 3 4 5 |
diag(7,3) [,1] [,2] [,3] [1,] 7 0 0 [2,] 0 7 0 [3,] 0 0 7 |

#### Identity Matrix

A scalar matrix for which k = 1 is called an

identity matrix.

In other words, a diagonal matrix has 1s on the diagonal. This type of matrix is abbreviated as I.

diag(k) creates a k x k identity matrix. For instance:

1 2 3 4 5 6 |
diag(3) [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 |

Identity matrices play a central role in linear algebra because they play a role similar to the number 1 in ordinary algebra.

### Triangular Matrices

A triangular matrix is one that has non-zero values only on one side of the diagonal.

*Note: In a strictly triangular matrix, the diagonal consists of zeroes as well*

#### Lower triangular matrix

It’s defined by:

For instance:

1 |
B<-matrix(c(1,0,0,2,4,0,3,1,2),nrow=3,ncol=3,byrow=T) |

[,1] [,2] [,3]

[1,] 1 0 0

[2,] 2 4 0

[3,] 3 1 2

#### Upper triangular matrix

It’s defined by:

For instance:

1 2 3 4 5 6 |
B<-matrix(c(1,2,3,0,4,5,0,0,2),nrow=3,ncol=3,byrow=T) [,1] [,2] [,3] [1,] 1 2 3 [2,] 0 4 5 [3,] 0 0 2 |