## Modification of the matrix

R allows us to do modification in the matrix. There are several methods to do modification in the matrix, which are as follows:

### Assign a single element

In matrix modification, the first method is to assign a single element to the matrix at a particular position. By assigning a new value to that position, the old value will get replaced with the new one. This modification technique is quite simple to perform matrix modification. The basic syntax for it is as follows:

matrix[n, m]<-y

Here, n and m are the rows and columns of the element, respectively. And, y is the value which we assign to modify our matrix.

Let see an example to understand how modification will be done:

**Example**

```
# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
ccol_names = c("col1", "col2", "col3")
R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print(R)
#Assigning value 20 to the element at 3d roe and 2nd column
R[3,2]<-20
print(R)
```

**Output**

```
> col1 col2 col3
> row1 5 6 7
> row2 8 9 10
> row3 11 12 13
> row4 14 15 16
>
> col1 col2 col3
> row1 5 6 7
> row2 8 9 10
> row3 11 20 13
> row4 14 15 16
```

### Use of Relational Operator

R provides another way to perform matrix medication. In this method, we used some relational operators like >, <, ==. Like the first method, the second method is quite simple to use. Let see an example to understand how this method modifies the matrix.

**Example 1**

`# Defining the column and row names. row_names = c("row1", "row2", "row3", "row4") ccol_names = c("col1", "col2", "col3") R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names)) print(R) #Replacing element that equal to the 12 R[R==12]<-0 print(R)`

**Output**

`col1 col2 col3 row1 5 6 7 row2 8 9 10 row3 11 12 13 row4 14 15 16 col1 col2 col3 row1 5 6 7 row2 8 9 10 row3 11 0 13 row4 14 15 16`

**Example 2**

```
# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
ccol_names = c("col1", "col2", "col3")
R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print(R)
#Replacing elements whose values are greater than 12
R[R>12]<-0
print(R)
```

**Output**

```
col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 12 13
row4 14 15 16
col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 12 0
row4 0 0 0
```

### Addition of Rows and Columns

The third method of matrix modification is through the addition of rows and columns using the cbind() and rbind() function. The cbind() and rbind() function are used to add a column and a row respectively. Let see an example to understand the working of cbind() and rbind() functions.

**Example 1**

`# Defining the column and row names. row_names = c("row1", "row2", "row3", "row4") ccol_names = c("col1", "col2", "col3") R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names)) print(R) #Adding row rbind(R,c(17,18,19)) #Adding column cbind(R,c(17,18,19,20)) #transpose of the matrix using the t() function: t(R) #Modifying the dimension of the matrix using the dim() function dim(R)<-c(1,12) print(R)`

**Output**

`col1 col2 col3 row1 5 6 7 row2 8 9 10 row3 11 12 13 row4 14 15 16 col1 col2 col3 row1 5 6 7 row2 8 9 10 row3 11 12 13 row4 14 15 16 17 18 19 col1 col2 col3 row1 5 6 7 17 row2 8 9 10 18 row3 11 12 13 19 row4 14 15 16 20 row1 row2 row3 row4 col1 5 8 11 14 col2 6 9 12 15 col3 7 10 13 16 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 5 8 11 14 6 9 12 15 7 10 13 16`