in

# Matrices in R

In this article, we will discuss in detail the storage and retrieval of the elements in Matrices in R

## Introduction to Matrices

A matrix is considered to be an ordered collection of data elements in a tabular format. The arrangement of elements may be in a square or rectangular format. The elements belong to the same data type. The matrices in R have the following properties :

1. Every matrix has a fixed number of rows and columns.
2. Every element in the matrix belongs to the same data type.
3. The order of the matrix is determined by the dimensions, that is row * columns

The primary advantage of storing data in this format is that it increases the readability of the data. Matrices can store logical, string values, but the primary usage of matrices is when they contain numerical data, which may belong to complex numbers, integers, or floating point numbers.

## Matrix Syntax in R

In the R programming language, matrices can be created by the following command :

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

The arguments involved in the method are :

data - It is the sequence of the data values that are to be arranged

• nrow - No of rows
• ncol - No of columns
1. byrow - By default, the elements of the matrix are arranged in column-wise order. If this parameter is set to TRUE, the elements are arranged in row-wise order.
2. dimnames - The names of the assigned dimensions to the matrix

## Examples of Matrices in R

The following code snippet illustrates the definition of the matrices in R :

```#declaring a matrix in R
mat <- matrix(1:12, nrow = 4)
#printing the matrix contents
print("Default column wise ordering")
print(mat)

#setting the byrow parameter of the matrix
mat <- matrix(1:12, nrow = 4, byrow = TRUE)
#printing the matrix contents
print("Row wise ordering")
print(mat)
```
##### The code produces different outputs when the byrow parameter is set :
``` "Default column wise ordering"
[,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

 "Row wise ordering"
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
```

## Accessing elements of a matrix in R

1. The singular elements of the matrix can be accessed by simply specifying the row and the column number using the indexing operator. The element position can be retrieved using the following command :
```#declaring a matrix in R
mat <- matrix(1:12, nrow = 4)
#printing the matrix contents
print("Printing the second row third column element from the matrix")
print(mat[2,3])
```
##### Output :
``` "Printing the second row third column element from the matrix"
 10
```
1. The entire row of the data frame can be accessed by using the row index in such a way,
`Mat [row-indx,]`
1. The entire column of the data frame can be accessed by using the column index in such a way,
`Mat [ , col-indx]`

### Complete Examlple

```#declaring a matrix in R
mat <- matrix(1:12, nrow = 4)
#printing the matrix contents
print(mat)
print("Accessing last row of the matrix ")
print(mat[4,])

print("Accessing the second column of the matrix")
print(mat[,2])

print("Accessing second to fourth row and second to third column of matrix ")
print(mat[2:4,2:3])
```
##### The code produces the following output :
```   [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
 "Accessing last row of the matrix "
  4  8 12
 "Accessing the second column of the matrix"
 5 6 7 8
 "Accessing second to fourth row and second to third column of matrix "
[,1] [,2]
[1,]    6   10
[2,]    7   11
[3,]    8   12```

## Written by Mallika Gupta

A software developer with a keen desire to explain in detail and with precision, every topic that I introduce to the user, be it in my content or my teaching. Enthusiastic about learning and sharing it.