# Matrix in R | How to Create, Access & Modify R Vectors

In this article, we will study what is a matrix in R, how to create a matrix in R, and access the elements in it. Also, we will study how to modify the matrix and create transpose a matrix. We will also go through creating names of rows and columns of the matrix. So, let's get started!

## What is Matrix in R?

A matrix is an R object with a two-dimensional rectangular layout in which all the elements are arranged. It is a data structure in the R programming language. The matrix contains dimensional attributes unlike vectors in R programming. Therefore all the attributes of the matrix can be checked with the attribute() function. Although we can create a matrix with only characters or only logical values, they are not much of any use. But if we create the matrix with numerical elements, we can use it in mathematical calculations.

## How to Create a Matrix in R?

To create a matrix in R language we need to use function matrix(). The set of elements in the vector are the arguments for this function.

Syntax:

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

The description of all the parameters above is given below:

• data: Input vectors as data elements of the matrix
• nrow: The number of rows that are to be created
• ncol: The number of columns that are to be created
• byrow: This parameter is a logical argument. If it is true then the vector elements are arranged by row.
• dimnames: This parameter assigns a name to row and column

For Example:

```M = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("The 3x3 matrix:\n")
print(M)
```

Output:

The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

## How to Access Matrix in R?

Elements in the matrix can be accessed by using column and row index of the element> Let us consider the matrix M above to access elements.

```M = matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3,	 ncol = 3, byrow = TRUE)
cat("The 3x3 matrix:\n")
print(M)

# Accessing elements
cat("Accessing first and second row\n")
print(M[1:2, ])
```

Output:

The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Accessing first and second row
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

## How to Modify Matrix in R?

In R programming, you can modify the elements of the matrices by a direct assignment.

```# Create a 3x3 matrix
M = matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("The Original Matrix:\n")
print(M)

# Editing the 2nd rows and 2nd column element
M[2, 2] = 21

cat("After modifing the matrix\n")
print(M)
```

Output:

The Original Matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

After modifying the matrix
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 21 6
[3,] 7 8 9

### How to Transpose a Matrix?

The operation of converting rows of the matrix into column and column into a row is called Transpose of a Matrix. To create a transpose of Matrix in R programming we use the t() function. The syntax of the transpose of a matrix is as follow:

Aij = Aji  where i is not equal to j

For example:

```# create a matrix with 2 rows
M <- matrix(1:6, nrow = 2)
print(M)

# using t() function to create transpose of matrix
t <- t(M)
print(t)
```

Output:

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

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

### How to name the rows and columns?

In R matrices, we use the rownames() and colnames() function to name the rows and columns of the matrix.

For example:

```# R program to create a matrix

M = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)

# Naming rows
rownames(M) = c("a", "b", "c")

# Naming columns
colnames(M) = c("c", "d", "e")

cat("The 3x3 matrix:\n")
print(M)
```

Output:

The 3x3 matrix:
c d e
a 1 2 3
b 4 5 6
c 7 8 9

## Conclusion

Therefore, in this article, we studied what is matrix in r and how it is created. Along with that we also learned the operations of accessing, modifying, and naming the rows and columns of matrices in R programming.

### FavTutor - 24x7 Live Coding Help from Expert Tutors! 