# 5 Matrix Functions in R You Should Know!

Filed Under: R Programming Hello, readers! In this article, we will be focusing on some of the important R Matrix functions, in detail. So, let us begin! 🙂

## Matrix in R programming

R programming offers us with multiple data structures to store and process the data values. One such data structure is Matrix in R.

With Matrix in R, we can store the data values in multiple dimensions. This offers us a huge variety of functions to deal with. In comparison to R Vectors, Matrices can be understood as Vectors being stored in multiple dimensions across it.

Matrix offers us with various in-built functions to deal with the data values and perform manipulations on the same.

In the course of this topic, we will be focusing on the below functions offered by Matrix to deal and manipulate the multi-dimensional data:

1. R solve() function
2. Transpose of a matrix with t() function
3. Joining rows and columns with rbind() & cbind() functions
4. Statistical mean operation on the matrix
5. The %*% operator

So, let us now have a look at them one by one.

## 1. The solve() Matrix function in R

R Matrix, as discussed above, stores the data in a multi-dimensional format. It offers us with solve() function that calculates the mathematical inverse of the matrix and returns the inverse matrix as a result.

Have a look at the below syntax!

Syntax:

```solve(matrix)
```

Example:

In the below example, we have created a matrix with data points 1,2,3,4, rows = 2 and columns = 2, respectively. Further, we have applied solve() function on the matrix to calculate and represent the inverse matrix from the original matrix.

```mat = matrix(c(1:4),c(2,2))
print("Original Matrix:")
print(mat)
sol <- solve(mat)
print("Inverse Matrix:")
print(sol)
```

Output:

As a result, the solve() function calculates the inverse of the matrix and results into a new inverse matrix.

``` "Original Matrix:"
> print(mat)
[,1] [,2]
[1,]    1    3
[2,]    2    4

 "Inverse Matrix:"
> print(sol)
[,1] [,2]
[1,]   -2  1.5
[2,]    1 -0.5
```

## 2. Transpose of a matrix

One of the mostly performed operation on the matrix in Transpose. We can replicate the same in R using t() function. With t() function, we can calculate the transpose of the matrix.

Syntax:

```t()
```

Example:

```mat = matrix(c(1:4),c(2,2))
print("Original Matrix:")
print(mat)
trans <- t(mat)
print("Transpose Matrix:")
print(trans)
```

Output:

``` "Original Matrix:"
> print(mat)
[,1] [,2]
[1,]    1    3
[2,]    2    4

 "Transpose Matrix:"
> print(trans)
[,1] [,2]
[1,]    1    2
[2,]    3    4
```

## 3. Function for joining matrix rows and columns in R

As the data gets stored in a matrix form, it gets structured into a row-column format. R Matrix offers us with rbind() and cbind() functions to perform operations on the rows and columns.

1. The rbind() function: It joins the matrices in a row-wise fashion.
2. The cbind() function: It performs the joining of the matrices in a column-wise fashion.

Have a look at the syntax!

```rbind(matrix,matrix)
cbind(matrix,matrix)
```

Example:

In the below example, we have performed a row-wise join of the two matrices (row below a row) using rbind() function. Further, we have also performed column-wise joining of matrices (column beside a column) using cbind() function.

```mat1 = matrix(c(1:4),c(2,2))
mat2 = matrix(c(2:5),c(2,2))
print("Column bind:")
print(cbind(mat1,mat2))
print("Row bind:")
print(rbind(mat1,mat2))
```

Output:

```> print("Column bind:")
 "Column bind:"
> print(cbind(mat1,mat2))
[,1] [,2] [,3] [,4]
[1,]    1    3    2    4
[2,]    2    4    3    5
> print("Row bind:")
 "Row bind:"
> print(rbind(mat1,mat2))
[,1] [,2]
[1,]    1    3
[2,]    2    4
[3,]    2    4
[4,]    3    5
```

## 4. Mean operation on rows and columns

R Matrix offers us with the below functions to perform statistical operations on the data such as mean, sum, etc —

1. colSums() – It calculates and returns the sum of every column in the matrix.
2. rowSums() – Calculates and returns the sum of every row in the matrix.
3. colMeans() – Returns the mean of every column of the matrix.
4. rowMeans() – Returns the mean value of every row of the matrix.
5. mean() – Returns the mean of the entire matrix.

Example:

```mat1 = matrix(c(1:4),c(2,2))
print(colSums(mat1))
print(rowSums(mat1))
print(colMeans(mat1))
print(rowMeans(mat1))
print(mean(mat1))
```

Output:

```> print(colSums(mat1))
 3 7
> print(rowSums(mat1))
 4 6
> print(colMeans(mat1))
 1.5 3.5
> print(rowMeans(mat1))
 2 3
> print(mean(mat1))
 2.5
```

## 5. The %*% Operator

The %*% operator enables us to perform the multiplication of the matrices entirely rather than in an element-wise fashion.

Syntax:

```matrix%*%matrix
```

Example:

```mat1 = matrix(c(1:4),c(2,2))
mat2 = matrix(c(2:5),c(2,2))
print(mat1%*%mat2)
```

Output:

```> print(mat1%*%mat2)
[,1] [,2]
[1,]   11   19
[2,]   16   28
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to R programming, Stay tuned with us!

Till then, Happy Learning!! 🙂

close
Generic selectors
Exact matches only
Search in title
Search in content