Linear Algebra with R – Part I – Introduction

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



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


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

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.


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:

Screen Shot 2015-02-25 at 18.27.15

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

Screen Shot 2015-02-25 at 15.46.29

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:

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

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

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

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:

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,

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

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:

For instance,

creates the following matrix:

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:

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:

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

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:

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


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 square matrix.

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

For instance:


Symmetric Matrices

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

Let’s see an example:

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 matrix is 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}

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.

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:

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:

Screen Shot 2015-02-25 at 20.16.36

For instance:

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

Upper triangular matrix

It’s defined by:

Screen Shot 2015-02-25 at 20.17.32

For instance:



Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS