Array vs Matrix in R Programming
Last Updated :
11 Jun, 2021
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:
Python3
# 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)
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:
Python3
# 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)
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 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. |
Similar Reads
Correlation Matrix in R Programming
Correlation refers to the relationship between two variables, specifically the degree of linear association between them. In R, a correlation matrix represents this relationship as a range of values between -1 and 1.A value of -1 indicates a perfect negative linear relationship.A value of 1 indicate
5 min read
Array Operations in R Programming
Arrays are the R data objects which store the data in more than two dimensions. Arrays are n-dimensional data structures. For example, if we create an array of dimensions (2, 3, 3) then it creates 3 rectangular matrices each with 2 rows and 3 columns. They are homogeneous data structures. Now, letâs
4 min read
Sorting of Arrays in R Programming
Prerequisite: R â Array A vector is a uni-dimensional array, which is specified by a single dimension, length. A Vector can be created using the âc()â function. A list of values is passed to the c() function to create a vector. Sorting can be done either in ascending order or descending. There are f
5 min read
Data Munging in R Programming
Data Munging is the general technique of transforming data from unusable or erroneous form to useful form. Without a few degrees of data munging (irrespective of whether a specialized user or automated system performs it), the data can't be ready for downstream consumption. Basically the procedure o
11 min read
Assigning Vectors in R Programming
Vectors are one of the most basic data structure in R. They contain data of same type. Vectors in R is equivalent to arrays in other programming languages. In R, array is a vector of one or more dimensions and every single object created is stored in the form of a vector. The members of a vector are
5 min read
Parallel Programming In R
Parallel programming is a type of programming that involves dividing a large computational task into smaller, more manageable tasks that can be executed simultaneously. This approach can significantly speed up the execution time of complex computations and is particularly useful for data-intensive a
6 min read
Data Reshaping in R Programming
Generally, in R Programming Language, data processing is done by taking data as input from a data frame where the data is organized into rows and columns. Data frames are mostly used since extracting data is much simpler and hence easier. But sometimes we need to reshape the format of the data frame
5 min read
Basic Syntax in R Programming
R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features. This art
3 min read
Data Structures in R Programming
A 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. Râs base data structures are often organized by
6 min read
R Programming for Data Science
R is an open-source programming language used statistical software and data analysis tools. It is an important tool for Data Science. It is highly popular and is the first choice of many statisticians and data scientists.R includes powerful tools for creating aesthetic and insightful visualizations.
13 min read