# Matrices Data Structure in R – R tutorial

238

Data in R is held as a wide variety of objects such as vectors, matrices, arrays, data frames, and lists. Let’s see How to create or assign a vector in R.

#### What is Matrix?

A matrix is a two-dimensional array where each element has the same data type (numeric,
character, or logical). We can create a matrix using matrix function `matrix`. The general format is

```myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,
byrow=logical_value, dimnames=list(char_vector_rownames, char_vector_colnames))```

where `vector` contains the elements for the matrix, `nrow` and `ncol` specify the row and column dimensions and `dimnames` contains optional row and column labels stored in character vectors. The option `byrow` indicates whether the matrix should be filled in by row (`byrow=TRUE`) or by column (`byrow=FALSE`). The default is by column. The following example demonstrates the `matrix` function.

##### Examples
```>  a <- matrix(10:30, nrow=5, ncol=4)
Warning message:
In matrix(10:30, nrow = 5, ncol = 4) :
data length  is not a sub-multiple or multiple of the number of rows 
>  a <- matrix(11:30, nrow=5, ncol=4)
> a
[,1] [,2] [,3] [,4]
[1,]   11   16   21   26
[2,]   12   17   22   27
[3,]   13   18   23   28
[4,]   14   19   24   29
[5,]   15   20   25   30
> data <- c(78,82,64,51)
> rownames <- c("Ramprakash","Sulthan")
> columnnames <- c("Maths","Science")
> marklist<-matrix(data,nrow=2,ncol=2,byrow=FALSE,dimnames=list(rownames,columnnames))
> marklist
Maths Science
Ramprakash    78      64
Sulthan       82      51```

Here, First, we create a 5×4 matrix `a`. Then we create a 2×2 matrix with labels and fill the matrix by rows. Finally, we create a 2×2 matrix and fill the matrix by columns.

#### How to use Matrix in R?

I will show you simple examples with standard functions exist in R for common mathematical operations on matrices.

#### Transpose a matrix in R

```> a <- matrix(11:30, nrow=5, ncol=4)
> a
[,1] [,2] [,3] [,4]
[1,]   11   16   21   26
[2,]   12   17   22   27
[3,]   13   18   23   28
[4,]   14   19   24   29
[5,]   15   20   25   30
> t(a)
[,1] [,2] [,3] [,4] [,5]
[1,]   11   12   13   14   15
[2,]   16   17   18   19   20
[3,]   21   22   23   24   25
[4,]   26   27   28   29   30```

#### Determinant of a matrix in R

```> a <- matrix(c(1:7,8,10),3,3)
> a
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6   10
> det(a)
 -3```

#### Diagonal of a matrix in R

```> a <- matrix(c(1:7,8,10),3,3)
> a
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6   10
> diag(a)
  1  5 10```

#### Inverse of a matrix in R

```> a <- matrix(c(1:7,8,10),3,3)
> a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 10
> solve(a)
[,1] [,2] [,3]
[1,] -0.6666667 -0.6666667 1
[2,] -1.3333333 3.6666667 -2
[3,] 1.0000000 -2.0000000 1```

Matrices are two-dimensional and, like vectors, can contain only one data type. When there are more than two dimensions, you have to use arrays. When there are multiple modes of data, you have to use data frames.