The data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values. The two most important data structures in R are Arrays and Matrices.

### Arrays in R

Arrays are data storage objects in R containing more than or equal to 1 dimension. Arrays can contain only a single data type. The

function is an in-built function which takes input as a vector and arranges them according to **array()****dim** argument. Array is an iterable object, where the array elements are indexed, accessed and modified individually. Operations on array can be performed with similar structures and dimensions. Uni-dimensional arrays are called vectors in R. Two-dimensional arrays are called matrices.

Syntax:

array(array1, dim = c (r, c, m), dimnames = list(c.names, r.names, m.names))

Parameters:

array1: a vector of values

dim: contains the number of matrices, m of the specified number of rows and columns

dimnames: contain the names for the dimensions

**Example**:

`# R program to illustrate an array ` ` ` `# creating a vector ` `vector1 <` `-` `c(` `"A"` `, ` `"B"` `, ` `"C"` `) ` `# declaring a character array ` `uni_array <` `-` `array(vector1) ` `print` `(` `"Uni-Dimensional Array"` `) ` `print` `(uni_array) ` ` ` `# creating another vector ` `vector <` `-` `c(` `1` `:` `12` `) ` `# declaring 2 numeric multi-dimensional ` `# array with size 2x3 ` `multi_array <` `-` `array(vector, dim ` `=` `c(` `2` `, ` `3` `, ` `2` `)) ` `print` `(` `"Multi-Dimensional Array"` `) ` `print` `(multi_array) ` |

*chevron_right*

*filter_none*

**Output:**

[1] "Uni-Dimensional Array" [1] "A" "B" "C" [1] "Multi-Dimensional Array" , , 1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 , , 2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12

### Matrices in R

Matrix in R is a table-like structure consisting of elements arranged in a fixed number of rows and columns. All the elements belong to a single data type. R contains an in-built function

to create a matrix. Elements of a matrix can be accessed by providing indexes of rows and columns. The arithmetic operation, addition, subtraction, and multiplication can be performed on matrices with the same dimensions. Matrices can be easily converted to data frames CSVs.**matrix()**

Syntax:

matrix(data, nrow, ncol, byrow)

Parameters:

data: contain a vector of similar data type elements.

nrow: number of rows.

ncol: number of columns.

byrow: By default matrices are in column-wise order. So this parameter decides how to arrange the matrix

**Example:**

`# R program to illustrate a matrix ` ` ` `A ` `=` `matrix( ` ` ` `# Taking sequence of elements ` ` ` `c(` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `, ` `7` `, ` `8` `, ` `9` `), ` ` ` ` ` `# No of rows and columns ` ` ` `nrow ` `=` `3` `, ncol ` `=` `3` `, ` ` ` ` ` `# By default matrices are ` ` ` `# in column-wise order ` ` ` `# So this parameter decides ` ` ` `# how to arrange the matrix ` ` ` `byrow ` `=` `TRUE ` `) ` ` ` `print` `(A) ` |

*chevron_right*

*filter_none*

**Output:**

[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9

#### Arrays vs Matrices

Arrays | Matrices |
---|---|

Arrays can contain greater than or equal to 1 dimensions. | Matrices contains 2 dimensions in a table like structure. |

Array is a homogenous data structure. | Matrix is also a homogenous data strucrure. |

It is a singular vector arranged into the specified dimensions. | It comprises of multiple equal length vectors stacked together in a table. |

function can be used to create matrix by specifying the third dimension to be 1. |
function however can be used to create at most 2-dimensional array. |

Arrays are superset of matrices. | Matrices are a subset, special case of array where dimensions is two. |

Limited set of collection-based operations. | Wide range of collection operations possible. |

Mostly, intended for storage of data. | Mostly, matrices are intended for data transformation. |

## Recommended Posts:

- Check if the Object is a Matrix in R Programming - is.matrix() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Convert an Object into a Matrix in R Programming - as.matrix() Function
- Transform the Scaled Matrix to its Original Form in R Programming - Using Matrix Computations
- Calculate the Sum of Matrix or Array columns in R Programming - colSums() Function
- Calculate the Mean of each Column of a Matrix or Array in R Programming - colMeans() Function
- Compute the Sum of Rows of a Matrix or Array in R Programming - rowSums Function
- Perform Operations over Margins of an Array or Matrix in R Programming - apply() Function
- Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
- Return a Matrix with Lower Triangle as TRUE values in R Programming - lower.tri() Function
- Compute Choleski factorization of a Matrix in R Programming - chol() Function
- Get or Set Dimensions of a Matrix in R Programming - dim() Function
- Create Matrix and Data Frame from Lists in R Programming
- Getting a Matrix of number of columns in R Programming - col() Function
- Calculate the cross-product of a Matrix in R Programming - crossprod() Function
- Calculate the cross-product of the Transpose of a Matrix in R Programming - tcrossprod() Function
- Getting the Determinant of the Matrix in R Programming - det() Function
- Construct a Diagonal Matrix in R Programming - diag() Function
- Find String Matches in a Vector or Matrix in R Programming - str_detect() Function
- Getting a Matrix of number of rows in R Programming - row() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.