Programming Code Examples

What is Matrix in R? Explained with Examples4 min read

In R, a matrix is a two-dimensional array of data, with rows and columns. Matrices are often used to store and manipulate data sets in a compact and efficient manner.

What is Matrix?

A matrix is a two-dimensional array of numbers, symbols, or expressions, arranged in rows and columns. In mathematics and computer science, matrices are widely used for data storage and manipulation, particularly for linear algebra and for transforming data in various ways.

Each element in a matrix is identified by its row and column indices, and can be manipulated and transformed as needed. Matrices can be added, subtracted, multiplied, and inverted, and they can also be used to represent linear transformations in 2D and 3D space.

Matrices have many applications, including image processing, computer graphics, machine learning, and optimization. In these fields, matrices are used to represent and manipulate large amounts of data in a compact and efficient manner, allowing for powerful data analysis and transformation.

A simple example of a matrix can be a table representing the grades of students in a class:

In this example, each row represents an exam, and each column represents a student. The element at the intersection of a row and column is the grade the student received on that exam. This matrix can be used to analyze the performance of each student and the class as a whole. For example, you could calculate the average grade for each student or the class by taking the mean of the values in each column or row.

All examples provided in this R Matrix are basic, simple, and easy to practice for beginners who are enthusiastic to learn R and advance their careers.

Create Matrix

Here’s an example of how you can create a matrix in R:

In this example, c(1,2,3,4,5,6) is a vector of data that will be stored in the matrix. nrow and ncol specify the number of rows and columns the matrix should have. byrow is set to TRUE, which means the data in the vector will be filled into the matrix by row.

You can access individual elements of the matrix using square brackets [], for example:

You can perform operations on matrices in R just like you would with vectors. For example, you can add, subtract, and multiply matrices:

Note that in matrix arithmetic, the dimensions of the matrices must match for operations to be performed.

You may also like: R Program to Multiply Two Matrix Using Multi-dimensional Arrays

Access Values of Matrix

In R, you can access the values of a matrix using square brackets [] and specifying the row and column indices.

For example, given a matrix mat:

To access the element in the first row and first column of mat, you can use the following syntax:

You can also access entire rows or columns of a matrix. For example, to access the first row of mat, use:

And to access the second column of mat, use:

You can also use row and column indices to extract submatrices from a matrix. For example, to extract the first two rows and first two columns of mat, use:

Note that row and column indices in R start from 1, not 0.

Modify Matrix Values

In R, you can modify the values of a matrix by assigning new values to individual elements or to entire rows or columns.

For example, given a matrix mat:

To modify the value of an individual element, use the square brackets [] to specify the row and column indices and assign a new value using the assignment operator <-:

This changes the value of the element in the first row and second column of mat to 9.

To modify an entire row or column, use the same syntax, but leave one of the indices blank to represent all elements in that row or column:

These operations change the values of the first row and third column of mat to the values specified in the vectors on the right-hand side of the assignments.

It’s important to note that when modifying a matrix in R, the dimensions of the assigned values must match the dimensions of the matrix being modified. For example, when assigning values to a row, the length of the assigned vector must match the number of columns in the matrix.


In conclusion, matrices are a powerful and versatile data structure in R. They allow you to store and manipulate data sets in a compact and efficient manner, and provide a range of operations for accessing, modifying, and transforming the data. Understanding how to create, access, and modify matrices is an essential skill for data analysis and manipulation in R.

Add comment