R

# Create Matrix in R

“A matrix is a data structure that is used to store and display organized data. It is also called a 2-dimensional or multidimensional array. It is just like a table with columns and rows. However, there is more flexibility in matrices than in tables. Tables, for example, usually have a fixed number of rows and columns, whereas the size of a matrix (rows and columns) can be modified. In programming languages, a matrix can be used to store a group of related data in it.

Matrixes, for example, are a data type that allows greater flexibility than a static array in some programming languages. Rather than storing values as distinct variables, values are stored in a matrix. This helps the computer to operate the operations more efficiently and effectively on the data. A matrix is capable of storing a single basic type of data (numeric, logical, character, etc.). Therefore, two or more vectors can be combined to make a matrix.”

## How to Use Matrices in R Programming Language in Ubuntu 20.04?

There are different types of matrices that you can create in R. In the following examples; you will learn how to create different types of arrays and how to specify names and parameters.

## Example # 1: Creating a Simple Matrix in R

To create a matrix in R, the matrix() function is used with some specified parameters. The basic syntax of the matrix function is:

Syntax:

matrix(data, byrow, nrow, ncol, dimnames)

Where data is the set of data to be passed, byrow is a logical variable to define whether the matrix will be row-wise or column-wise (matrices are by default column-wise), nrow and ncol are representing the number of rows and columns, respectively, .dimnames  are used for naming the dimensions of the specified matrix.

In the example above, we have created a dataset and assigned it to variable values, which will be used as a parameter in the matrix function. The values of nrow and ncol are specified as 3, so the matrix will be 3×3. Making byrow = True will allow us to create the row-wise matrix.

To display the matrix on the console, we can use the print() function. The matrix m1 will be passed as an argument in the print function. As you can see, our 3×3 matrix has been created. The values in the matrix are taken row-wise from the dataset.

## Example # 2: Creating Matrix Using cbind() and rbind() Function

In R, matrices can be created using two or more vectors. One method to create a matrix using vectors is by using cbind and rbind functions. It is obvious by their names the cbind will bind two vectors column-wise, whereas the r bind will bind the vectors row-wise to create a matrix.

First, we will create a matrix using the cbind() function.

In this example, we have created three vectors, v1, v2, and v3. To create the matrix, we have to pass the vector as input in the cbind() function. This function will take the elements of the vector in the form of rows one by one.

Now let’s use the same vectors in rbind() to see the results.

This time the vectors were the same as used in the previous example with cbind(), but the values were taken in a column-wise manner.

## Example # 3: Creating a Matrix With the Vector Using the dim() Function

The other method of creating a matrix by using a vector is by specifying its dimensions. The dim() function will be used for creating matrices using vectors.

We will create a vector having some values. These values should be equal to the specified dimensions of the matrix. After creating the matrix, we will pass the matrix as an input in the dim() function. Here, we will assign the dimensions for the vector. As you can see from the example, we have specified the rows and columns, respectively (2, 3). That means the matrix will have 2 rows and 3 columns.

## Example # 4: Creating a Special Matrix

R allows the creation of different types of matrices. Following are some types of matrices.

## Matrix With the Same Element

To create a matrix that consists of the same element in the rows and columns, you can use the following syntax.

Syntax:

matrix(c, r, m)

If we specify the constant as a single value with a specified number of rows, we will get a single-valued matrix with multiple rows and columns.

In this example, we have specified the constant equal to 7 and rows and columns equal to 3. As only one value is specified, the matrix repeats the same elements in the rows and columns to fill the matrix.

## Diagonal Matrix

The matrix in which all the entries are 0s except the main diagonal entries. To create a diagonal matrix, the diag() function is used in R. The number of rows and columns must be the same in the diagonal matrix.

Syntax:

diag(c, r, m)

In the diag() function, we have passed a vector to fill the main diagonal, i.e. (4, 1, 6). The rows and columns are specified as 3. So the 3×3 diagonal matrix will be created as shown above.

## Identity Matrix

It is just like a normal diagonal matrix; the only difference is that the main diagonal entries are 1s in the identity matrix. To create an identity matrix, we will specify the constant equal to 1 in the parameter of the diag() function. The number of rows and columns, on the other hand, can be changed.

In the diag() function, we have specified the main diagonal equal to 1. The total columns and rows are specified as 4. So it will create a 4×4 identity matrix as shown above.

## Conclusion

In this tutorial, we have seen that the matrices are the data structure to store data of the same type. We learned about matrices in R, how to create matrices programming language, what their types are, and how to create different types of matrices. Now you should be familiar with the matrix functions and their parameters. We have also seen how to create matrices from vectors and what parameters and functions we can use to create and modify the matrices. After covering this tutorial, we hope that you have a better understanding of matrices in R.