R programming language has many data types like Vectors, Lists, Factors, DataFrame, Matrix, and Arrays. In this article, we will focus on the Matrix Data Type. We will see how to create, read, edit, and delete elements from the Matrix. Let’s dive.

**What is R Matrix**

R Matrix is a vector with attributes of a dimension and optionally, dimension names attached to the Vector. Matrix is a two-dimensional data structure in R programming.

A matrix in R is a collection of elements arranged in a two-dimensional rectangular layout.

**How to Create a Matrix in R**

To create a matrix in R, use the **matrix() **function. The dimension of the Matrix is defined by **nrow **and **ncol **property.

**Syntax**

`matrix(values, nrow=3, ncol=3)`

**Parameters**

The first parameter **values** are required, containing the values that need to be filled in the Matrix.

The **nrow **and **ncol **parameters can be optional if one is provided. For example, if your Matrix’s length is 9 and if you give the **nrow =3** then you don’t have to provide **ncol**. It will take **ncol = 3** automatically.

**Return Value**

The **matrix()** function returns the Matrix of provided values and dimensions.

You can also create a matrix using Vector. Create a file called **data.R **in your project directory.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3, ncol = 3)
print(mtrx)
```

If you are using terminal or CMD, then to run the R file, use the following command.

`Rscript data.R`

You will see the following (3 * 3) matrix.

```
Rscript data.R
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
```

We can also create the 3*3 Matrix using numbers.

```
mtrx <- matrix(1:9, nrow = 3, ncol = 3)
print(mtrx)
```

**Output**

```
Rscript data.R
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
```

There is not necessary to provide both dimension value. If one of the values of the dimension is provided, then the other will take from the length of the data.

```
mtrx <- matrix(1:9, nrow = 3)
print(mtrx)
```

**Output**

```
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
```

In this code, you can see that we did not provide the **ncol** value but still from the length of the Matrix, which is 9. It assumes that if the row is 3, then the column will be 3 as well.

Here, you can see that the Matrix is filled with column-wise. Means 1, 2, 3 is in one column, then 4, 5, 6 is in the second column.

We can change this order and fill the matrix row-wise.

```
mtrx <- matrix(1:9, nrow = 3, byrow = TRUE)
print(mtrx)
```

**Output**

```
[,1][,2][,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
```

You can see that now the Matrix is filled with row-wise values.

**How to assign columnnames and rownames in Matrix**

To assign columnnames and rownames, use the **dimnames **property. We can name the columns and rows by passing a two-element list to the argument dimnames.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3, dimnames = list(c("C1", "C2", "C3"), c("S1", "S2", "S3")))
print(mtrx)
```

**Output**

```
S1 S2 S3
C1 "Millie" "Sadie" "Winona"
C2 "Noah" "Gaten" "David"
C3 "Finn" "Caleb" "Natalia"
```

You can see that we successfully assigned the column and row names to the Matrix in R.

To access row names and column names in Matrix, use the **rownames()** and **colnames()** method.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3, dimnames = list(c("C1", "C2", "C3"), c("S1", "S2", "S3")))
print(colnames(mtrx))
print(rownames(mtrx))
```

**Output**

```
[1] "S1" "S2" "S3"
[1] "C1" "C2" "C3"
```

If you run the above file in the terminal, then you will see that print() method prints [1] by default. But we only need to focus on the output, which is the row names and column names. You can see that we get the column names and row names.

To change the column names and row names, we can reassign the values using the following syntax.

```
colnames(x) <- c("C1","C2","C3")
rownames(x) <- c("R1","R2","R3")
```

See the following example.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3, dimnames = list(c("C1", "C2", "C3"), c("S1", "S2", "S3")))
print(colnames(mtrx))
print(rownames(mtrx))
cat("After changing the names of row and column\n")
colnames(mtrx) <- c("K1", "K2", "K3")
rownames(mtrx) <- c("R1", "R2", "R3")
print(colnames(mtrx))
print(rownames(mtrx))
```

**Output**

```
[1] "S1" "S2" "S3"
[1] "C1" "C2" "C3"
After changing the names of row and column
[1] "K1" "K2" "K3"
[1] "R1" "R2" "R3"
```

The cat() is an inbuilt method in R that converts its arguments to character strings, concatenates them, separating them by the given sep= string, and then prints them. There is no linefeeds are printed unless explicitly requested by “\n” or if generated by filling (if argument fill is TRUE or numeric.) The cat() method is handy for producing output in user-defined functions.

**Create Matrix in R using cbind() and rbind()**

You can also create a matrix in R is by using the two functions **cbind()** and **rbind()** as in column bind and row bind.

```
matrixC <- cbind(c(11, 21, 31), c(41, 51, 61))
cat("Print column-binded Matrix\n")
print(matrixC)
matrixR <- rbind(c(71, 81, 91), c(100, 110, 120))
cat("Print row-binded Matrix\n")
print(matrixR)
```

**Output**

```
Print column-binded Matrix
[,1] [,2]
[1,] 11 41
[2,] 21 51
[3,] 31 61
Print row-binded Matrix
[,1] [,2] [,3]
[1,] 71 81 91
[2,] 100 110 120
```

You can see that the first Matrix is the **column-binded,** and the second Matrix is **row-binded**.

**How to access elements of a Matrix in R**

To access elements of a Matrix in R, use one of the following methods.

- Using integer vector as an index
- Using a logical vector as an index
- Using character vector as an index

Let’s see the methods one by one.

**Using integer vector as an index**

To access elements of Matrix, use the integer vector as an index. You can specify the row numbers and column numbers as vectors and use them for indexing. If any field inside a bracket is left blank, that means it selects all. We can use the negative integers to define rows or columns to be excluded.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("---------------------------------------------------\n")
cat("Select the elements of rows 1 and 2 and cols 2 an 3\n")
print(mtrx[c(1, 2), c(2, 3)])
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
---------------------------------------------------
Select the elements of rows 1 and 2 and cols 2 an 3
[,1] [,2]
[1,] "Sadie" "Winona"
[2,] "Gaten" "David"
```

You can see from the output that we have selected the values between row 1 and 2 and column 2 and 3.

Let’s leave the row blank and select columns.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("--------------------------------------------\n")
cat("Select the elements using index column(1, 2)\n")
print(mtrx[, c(1, 2)])
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
--------------------------------------------
Select the elements using index column(1, 2)
[,1] [,2]
[1,] "Millie" "Sadie"
[2,] "Noah" "Gaten"
[3,] "Finn" "Caleb"
```

From the above output, you can see that we have selected columns 1 and 2 and left rows blank. If you leave blank either row or column, then it will select everything.

Let’s leave the column and select the rows.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("-----------------------------------------\n")
cat("Select the elements using index row(1, 2)\n")
print(mtrx[c(1, 2), ])
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
----------------------------------------
Select the elements using index row(1, 2)
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
```

In this example, we have selected rows 1 and 2 and left the column blank. So it returns row 1 and 2 and all the columns.

Let’s select all the columns except the last column.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("------------------------------------------\n")
cat("Select all the elements except last column\n")
print(mtrx[, -1])
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
------------------------------------------
Select all the elements except last column
[,1] [,2]
[1,] "Sadie" "Winona"
[2,] "Gaten" "David"
[3,] "Caleb" "Natalia"
```

To access the first row of the Matrix, use the Matrix **[1, ]**. See the following example.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("--------------------\n")
cat("Select the first row\n")
print(mtrx[1, ])
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
--------------------
Select the first row
[1] "Millie" "Sadie" "Winona"
```

You can see that we got the only first row of the Matrix.

To select the first element of the Matrix, use the Matrix **[1]**.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
cat("Select first element\n")
print(mtrx[1])
```

**Output**

```
Select first element
[1] "Millie"
```

You can see that we got the first element of the Matrix.

**Using a logical vector as an index**

We can use the two logical vectors as an index of the Matrix. In that scenario, rows and columns where the value is **TRUE** are returned. These indexing vectors are recycled if required and can be mixed with integer vectors.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("-------------------\n")
print(mtrx[c(TRUE, FALSE, TRUE), c(FALSE, TRUE, TRUE)])
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
-------------------
[,1] [,2]
[1,] "Sadie" "Winona"
[2,] "Caleb" "Natalia"
```

You can also index using a single logical vector. See the following code.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("-------------------\n")
print(mtrx[c(TRUE, FALSE)])
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
-------------------
[1] "Millie" "Finn" "Gaten" "Winona" "Natalia"
```

In the above example, the matrix **mtrx** is treated as a vector formed by stacking columns of the Matrix one after another.

**Using character vector as an index**

You can index with a character vector for a matrix with a named row or column. This can be mixed with an integer or logical indexing.

**How to modify a matrix in R**

To modify the Matrix, first, we will access the matrix elements and then use the assignment operator to assign the new values to an item.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("---------------------------------------------------\n")
cat("Modify the 'Gaten' element and replace with 'Brett'\n")
mtrx[2, 2] <- "Brett"
print(mtrx)
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
---------------------------------------------------
Modify the 'Gaten' element and replace with 'Brett'
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Brett" "David"
[3,] "Finn" "Caleb" "Natalia"
```

In this example, we have modified the Matrix by replacing **Gaten **with **Brett.**

A standard operation with a matrix is to transpose it.

In transpose of Matrix, rows become columns, and columns become rows.

You can create a transpose of a matrix using method **t()** in R lang.

```
vt <- c("Millie", "Noah", "Finn", "Sadie", "Gaten", "Caleb", "Winona", "David", "Natalia")
mtrx <- matrix(vt, nrow = 3)
print(mtrx)
cat("---------------------------------------------------\n")
cat("Transpose the matrix using t()'\n")
t(mtrx)
```

**Output**

```
[,1] [,2] [,3]
[1,] "Millie" "Sadie" "Winona"
[2,] "Noah" "Gaten" "David"
[3,] "Finn" "Caleb" "Natalia"
---------------------------------------------------
Transpose the matrix using t()'
[,1] [,2] [,3]
[1,] "Millie" "Noah" "Finn"
[2,] "Sadie" "Gaten" "Caleb"
[3,] "Winona" "David" "Natalia"
```

You can see that t() method prints the transpose of the Matrix.

That is it for Matrix in R Lang.

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.