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.

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


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 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.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


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.
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.



My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.