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 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.|
|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.|
- 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 firstname.lastname@example.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.