A matrix, as we all know, is a rectangular array arranged in rows and columns.

In terms of programming language, matrix is a collection of data-elements arranged in a two-dimensional rectangular form. In this article, we will know all about creating matrices in R, and we will be using different methods here to help you choose the most appropriate method to create matrices in R as per your requirement.

- Creating a matrix in R using matrix() function
- Creating a matrix in R using matrix() function and giving names to rows and columns
- Creating a matrix in R by recycling the elements.
- Creating a matrix in R using rbind() function
- Creating a matrix in R using cbind() function

## Creating a matrix in R using matrix() function

**matrix()** function creates matrices in R programming language.

**Syntax:-**

*matrix(x, nrow, ncol, byrow, dimnames)*

**Arguments:-**

**x** – is a vector (including a list or expression vector) input representing the elements in the matrix.

**nrow** – specifies the number of rows in the matrix to be created.

**ncol** – specifies the number of columns in the matrix to be created.

**byrow** â€“ is a logical value if ** TRUE**: the matrix is filled row-wise; else will be filled column-wise. By default, its value is

*.*

**FALSE****dimnames**– is the argument to set the names of rows and columns.

**Example:-**

Below example code creates a matrix with elements from 1 to 16.

matrix1 <- matrix(c(1:16),nrow = 4,ncol=4) print(matrix1)

**Output:-Â **

[,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16

Now, we will see the usage of argument * byrow*. In the below example code, we are setting its value

*. So that the matrix is filled row-wise.*

**TRUE****Example:-**

matrix2 <- matrix(c(1:16),nrow = 4,ncol=4,byrow = TRUE) print(matrix2)

**Output:-**

[,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 [4,] 13 14 15 16

## Creating a matrix in R using matrix() function and naming rows and columns

Again, there are different ways to define the names and columns of the matrix. We will be talking about two of them.

Letâ€™s start with an example where we will be using the functions * rownames()* and

**colnames()****Syntax:-Â **

*rownames(matrix) <- valueÂ ***â€˜orâ€™** *row.names(matrix) <- value *

*colnames(matrix) <- valueÂ *

**Example:-**

matrix1 <- matrix(c(1:16),nrow = 4,ncol=4) rownames(matrix1) <- c("ROW_A","ROW_B","ROW_C","ROW_D") colnames(matrix1) <- c("COL_A","COL_B","COL_C","COL_D") print(matrix1)

**Output:-Â **

COL_A COL_B COL_C COL_D ROW_A 1 5 9 13 ROW_B 2 6 10 14 ROW_C 3 7 11 15 ROW_D 4 8 12 16

Another way to define the names of rows and columns is by using the * dimnames* arguments of the matrix() function.

The below example code specifies the usage of the list with this argument.

**Example:-**

matrix1 <- matrix(c(1:16),nrow = 4,ncol=4,dimnames = list(c("ROW_A","ROW_B","ROW_C","ROW_D"),c("COL_A","COL_B","COL_C","COL_D"))) print(matrix1)

**Output:-**

COL_A COL_B COL_C COL_D ROW_A 1 5 9 13 ROW_B 2 6 10 14 ROW_C 3 7 11 15 ROW_D 4 8 12 16

## Creating a matrix in R by recycling the elements.

If we need to create a matrix with fewer elements, then we can do the same using the below example code. Here, the elements are 1,2,3,4,5 will create the matrix recycling the elements every new column.

**Example:-**

matrix_recycle <- matrix(1:5, nrow = 5, ncol = 5) print(matrix_recycle)

**Output:-**

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

## Creating a matrix in R by using rbind() function.

**rbind()** function is another method to create the matrix where the elements are filled row-wise.

**Syntax:-**

*rbind(â€¦.,deparse.level)*

**Arguments:-**

**â€¦â€¦** – can be a vector or a matrix or a dataframe.

**deparse.level** – integer responsible for the construction of labels if non-matrix-like arguments are passed. *deparse.level =0* : constructs no labels; the default value is 0. *deparse.level = 1 or 2* constructs labels from the argument names.

**Example:-**

The below example code will create a matrix using * rbind() *function and

**deparse.level = o**code <- c(1,4,9,16) price<-c(25,50,75,100) matrix_rbind1 <- rbind(code,price,deparse.level = 0) print(matrix_rbind1)

**Output:-**

[,1] [,2] [,3] [,4] [1,] 25 50 75 100 [2,] 1 4 9 16

Observe in the output that the rows do not have the names.

Now, let us now see the difference on creating a matrix with * rbind() *function and

**deparse.level = 1****Example:-**

code <- c(1,4,9,16) price<-c(25,50,75,100) matrix_rbind2 <- rbind(code,price,deparse.level = 1) print(matrix_rbind2)

**Output:-**

[,1] [,2] [,3] [,4] code 1 4 9 16 price 25 50 75 100

Observe in the output that now the rows are named ‘*code’* and ‘*price’* respectively.

## Creating a matrix in R by using cbind() function.

**cbind()** function is another method to create a matrix where the elements are filled column-wise.

**Syntax:-**

*rbind(â€¦.,deparse.level)*

**Arguments:-**

**â€¦â€¦** – can be a vector or a matrix or a dataframe.

**deparse.level** – integer responsible for the construction of labels if non-matrix-like arguments are passed. *deparse.level =0* : constructs no labels; the default value is 0.Â *deparse.level = 1 or 2* constructs labels from the argument names.

**Example:-**

code <- c(1,4,9,16) price<-c(25,50,75,100) matrix_cbind1 <- cbind(code,price,deparse.level = 0) print(matrix_cbind1)

**Output:-**

[,1] [,2] [1,] 1 25 [2,] 4 50 [3,] 9 75 [4,] 16 100

Now, let us now see the difference on creating a matrix with c* bind() *function and

**deparse.level = 1****Example:-Â **

code <- c(1,4,9,16) price<-c(25,50,75,100) matrix_cbind2 <- cbind(code,price,deparse.level = 1) print(matrix_cbind2)

**Output:-Â **

code price [1,] 1 25 [2,] 4 50 [3,] 9 75 [4,] 16 100

Observe in the output that now the rows are named ‘*code’* and ‘*price’* respectively.

I hope this article helps you to build an understanding towards creating matrices in R. Happy coding.