The **diagonal **of a matrix refers to the elements along the diagonal entries of the matrix.

Since matrices are used in nearly all statistical algorithms and formulas, you may often have a need to either extract or replace the elements along the diagonal of a matrix.

The easiest way to do so is by using the **diag()** function from base R, which is designed for this exact task.

The **diag()** function uses the following basic syntax:

**diag(x = 1, nrow, ncol, names = TRUE)**

where:

**x**: Name of the matrix**nrow, ncol**: Optional dimensions for the result when x is not a matrix**names**: TRUE or FALSE to indicate if resulting matrix should inherit names from the dimensions of the original matrix

There are two common ways to use the **diag()** function in practice in R:

**Method 1: Use diag() to Retrieve Elements from Diagonal of Matrix**

#extract diagonal elements from matrix named my_matrix diag(my_matrix)

This particular example uses the **diag()** function to simply extract the diagonal elements from the matrix named **my_matrix**.

**Method 2: Use diag() to Set Elements on Diagonal of Matrix**

#set diagonal elements of matrix named my_matrix to zero diag(my_matrix) <- 0

This particular example uses the **diag()** function to set the values of each of the diagonal elements of the matrix named **my_matrix** to be zero.

The following examples show how to use each of these methods in practice.

**Note**: The **diag()** function is typically used when working with square matrices (matrices that have the same number of columns and rows) but it will still return the diagonal elements of a non-square matrix without causing any errors in your code.

**Example 1: Use diag() to Retrieve Elements from Diagonal of Matrix**

Suppose we create the following matrix named **my_matrix** that contains 3 rows and 3 columns:

#create 3x3 matrix my_matrix <- matrix(c(2, 5, -3, 0, 2, 6, 5, 5, 8), nrow=3) #view matrix my_matrix [,1] [,2] [,3] [1,] 2 0 5 [2,] 5 2 5 [3,] -3 6 8

Suppose that we would like to extract only the elements along the diagonal of the matrix.

We can use the **diag()** function with the following syntax to do so:

#extract values along the diagonal of the matrix diag(my_matrix) [1] 2 2 8

This returns the values **2**, **2** and **8**, which represent the three values along the diagonal of the matrix.

**Example 2: Use diag() to Set Elements on Diagonal of Matrix**

Suppose we create the following matrix named **my_matrix** that contains 3 rows and 3 columns:

#create 3x3 matrix my_matrix <- matrix(c(2, 5, -3, 0, 2, 6, 5, 5, 8), nrow=3) #view matrix my_matrix [,1] [,2] [,3] [1,] 2 0 5 [2,] 5 2 5 [3,] -3 6 8

Suppose that we would like to set the values of the elements along the diagonal of the matrix to all be equal to zero.

We can use the **diag()** function with the following syntax to do so:

#set all elements along diagonal of matrix to be zero diag(my_matrix) <- 0 #view updated matrix my_matrix [,1] [,2] [,3] [1,] 0 0 5 [2,] 5 0 5 [3,] -3 6 0

Notice that each of the values along the diagonal of the matrix are now equal to zero, just as we specified.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Sort a Matrix in R

How to Remove NA from Matrix in R

How to Convert Data Frame to Matrix in R

How to Convert a Table to a Matrix in R