## R Matrix

### Matrix :

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

- Matrix is a collection of vectors of same length

Consider a matrix **‘mat1’**

Syntax

mat1
class(mat1)
[1] “matrix”

**Output :**

### Creating Matrix

A matrix can be created in various ways

- by using matrix() function

Syntax

//Syntax:
matrix(data = x , nrow = ‘no. of rows’, ncol = ‘no. of columns’, byrow = TRUE/ FALSE, dimnames = “names for col and row’ )

Consider a hospital which has 4 general physicians for consulting. We need to distribute people with the following age groups among these four physicians

Syntax

ageGroup<-c(22,51,60,23,25,36,34,62,44,49,43,52,12,24,45,33,43,19,20,13,53,23,37,34)

We can convert the ageGroup vector to a matrix using the **matrix()** function and name the columns and rows accordingly

CODE/PROGRAM/EXAMPLE

ageGroup<-c(22,51,60,23,25,36,34,62,44,49,43,52,12,24,45,33,43,19,20,13,53,23,37,34)
length(ageGroup)
[1] 24
#Create 4 queues for each physician namely A, B, C, D
ageGroup.mat <- matrix(data = ageGroup, ncol = 4, byrow = FALSE,
dimnames = list(c("R1","R2","R3","R4","R5","R6"),
c("A","B","C","D")))
ageGroup.mat

**Output :**

by using **rbind() and cbind()**

**rbind() and cbind()** functions bind different vectors of equal length as per rows and columns respectively

Syntax

//Syntax:
x.mat <- rbind(a, b, c) or cbind(a, b, c)

where x is the new matrix object

a, b, c are vectors of equal length and of homogeneous type

Consider a scenario where there is a need to organize the GMAT scores of 4 members into “English, Math and Science” categories. Create a matrix from the below vectors, using rbind() and cbind() functions

CODE/PROGRAM/EXAMPLE

Jane <- c(99,96,99.2)
Tom <- c(99.8,98.3,99.7)
Katy <- c(99.4, 99.2, 98.9)
James <- c(98.3,99.1,99.9)

### Using rbind() function:

CODE/PROGRAM/EXAMPLE

GMAT.mat <- rbind(Jane,Tom,Katy,James)
colnames(GMAT.mat) <- c("English","Math","Science")
GMAT.mat

**Output :**

**Note :** Once a matrix is created using either **rbind() or cbind()**, it can be converted to the other respective matrix by using the transpose function, **t()**

**colnames() and rownames()** functions are used to view or modify the column and row names respectively

### dim()

dim() is used to view or modify the dimensions of a matrix or a data frame

Syntax

//Syntax:
dim(x) <- c(a,b)

where x can be vector, list, factor, matrix or data frame

a, b are the dimensions of x

Consider the previous example where people got divided into 4 queues as per the physicians

Say, one of the physician leaves. Now, if we want to divide the ageGroup matrix to 3 groups, use **dim()** function

CODE/PROGRAM/EXAMPLE

dim(ageGroup) <- c(8,3)
ageGroup
class(ageGroup)
[1] “matrix”

### Addressing Matrix

Values in a matrix can be retrieved by declaring an index inside a single square bracket “**[ ]**” operator

Syntax

//Syntax:
matrix_name[x,y]

where x, y specify the dimensions of the matrix

Consider the GMAT matrix

If we want to retrieve the Math score of Katy

CODE/PROGRAM/EXAMPLE

GMAT.mat[2,3]
[1] 99.2

If we want to modify the Math score of Katy

CODE/PROGRAM/EXAMPLE

GMAT.mat[2,3] <- 99.8
GMAT.mat

**Output :**