Array vs Matrix in R Programming
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 array() function is an in-built function which takes input as a vector and arranges them according to 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.
array(array1, dim = c (r, c, m), dimnames = list(c.names, r.names, m.names))
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
 "Uni-Dimensional Array"  "A" "B" "C"  "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 matrix() 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(data, nrow, ncol, byrow)
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
[,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 homogeneous data structure. Matrix is also a homogeneous data structure. It is a singular vector arranged into the specified dimensions. It comprises of multiple equal length vectors stacked together in a table. array() function can be used to create matrix by specifying the third dimension to be 1. matrix() 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.