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 see how to create arrays in R. To create an array in R you need to use the function called array(). The arguments to this array() are the set of elements in vectors and you have to pass a vector containing the dimensions of the array.
Array_NAME <- array(data, dim = (row_Size, column_Size, matrices, dimnames)
where,
data – An input vector given to the array.
matrices – Consists of multi-dimensional matrices.
row_Size – Number of row elements that an array can store.
column_Size – Number of column elements that an array can store.
dimnames – Used to change the default names of rows and columns according to the user’s preference.
Example:
vector1 < - c( 1 , 2 , 3 )
vector2 < - c( 10 , 15 , 3 , 11 , 16 , 12 )
result < - array(c(vector1, vector2), dim = c( 3 , 3 , 2 ))
print (result)
|
Output:
, , 1
[,1] [,2] [,3]
[1,] 1 10 11
[2,] 2 15 16
[3,] 3 3 12
, , 2
[,1] [,2] [,3]
[1,] 1 10 11
[2,] 2 15 16
[3,] 3 3 12
Operations on Arrays
Naming columns and rows
We can give names to the rows and columns using dimnames.
Example:
vector1 < - c( 1 , 2 , 3 )
vector2 < - c( 10 , 15 , 3 , 11 , 16 , 12 )
column.names < - c( "COLUMN1" , "COLUMN2" , "COLUMN3" )
row.names < - c( "ROW1" , "ROW2" , "ROW3" )
matrix.names < - c( "Matrix.NO1" , "Matrix.NO2" )
result < - array(c(vector1, vector2), dim = c( 3 , 3 , 2 ),
dimnames = list (row.names, column.names, matrix.names))
print (result)
|
Output:
, , Matrix.NO1
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
, , Matrix.NO2
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
Manipulating array elements
An array is made up of multiple dimensions and the operations are carried out by accessing elements.
Example:
vector1 < - c( 1 , 2 , 3 )
vector2 < - c( 4 , 6 , 8 , 0 , 2 , 4 )
array1 < - array(c(vector1, vector2), dim = c( 3 , 3 , 2 ))
vector3 < - c( 3 , 2 , 1 )
vector4 < - c( 2 , 4 , 6 , 8 , 3 , 5 )
array2 < - array(c(vector3, vector4), dim = c( 3 , 3 , 2 ))
matrix1 < - array1[,, 2 ]
matrix2 < - array2[,, 2 ]
result < - matrix1 + matrix2
print (result)
|
Output:
[,1] [,2] [,3]
[1,] 4 6 8
[2,] 4 10 5
[3,] 4 14 9
Accessing Array elements
Using index position in matrix any element can be accessed easily. Also, we can alter/change the element in an array using index position.
Syntax:
Array_Name[row_position, Column_Position, Matrix_Level]
Example:
vector1 < - c( 1 , 2 , 3 )
vector2 < - c( 10 , 15 , 3 , 11 , 16 , 12 )
column.names < - c( "COLUMN1" , "COLUMN2" , "COLUMN3" )
row.names < - c( "ROW1" , "ROW2" , "ROW3" )
matrix.names < - c( "Matrix.NO1" , "Matrix.NO2" )
result < - array(c(vector1, vector2), dim = c( 3 , 3 , 2 ),
dimnames = list (row.names, column.names, matrix.names))
print (result)
print (result[ 3 ,, 2 ])
|
Output:
, , Matrix.NO1
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
, , Matrix.NO2
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
COLUMN1 COLUMN2 COLUMN3
3 3 12
Calculation across array element
apply() function is used for calculations across array elements.
Syntax:
apply(x, margin, fun)
where,
x – an array.
margin – name of the dataset used.
fun – function to be applied to the elements of the array.
Example:
vector1 < - c( 3 , 2 , 1 )
vector2 < - c( 2 , 4 , 6 , 8 , 0 , 1 )
new.array < - array(c(vector1, vector2), dim = c( 3 , 3 , 2 ))
print (new.array)
result < - apply (new.array, c( 1 ), sum )
print (result)
|
Output:
, , 1
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 2 4 0
[3,] 1 6 1
, , 2
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 2 4 0
[3,] 1 6 1
[1] 26 12 16
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!