Array Operations in R Programming - GeeksforGeeks

22 Apr, 2020
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:
# Create the vectors with different lengthvector1 <- c(1, 2, 3)vector2 <- c(10, 15, 3, 11, 16, 12)  # taking this vector as inputresult <- 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

We can give names to the rows and columns using dimnames.Example:
# Creating Vectorsvector1 <- c(1, 2, 3)vector2 <- c(10, 15, 3, 11, 16, 12)  # Giving Names to rows and columnscolumn.names <- c("COLUMN1", "COLUMN2", "COLUMN3")row.names <- c("ROW1", "ROW2", "ROW3")matrix.names <- c("Matrix.NO1", "Matrix.NO2")  # taking this vector as inputresult <- 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

An array is made up of multiple dimensions and the operations are carried out by accessing elements.
Example:
# creating two vectors of different length# and taking vector as inputvector1 <- c(1, 2, 3)vector2 <- c(4, 6, 8, 0, 2, 4)array1 <- array(c(vector1, vector2), dim = c(3, 3, 2))  # creating other arrayvector3 <- c(3, 2, 1)vector4 <- c(2, 4, 6, 8, 3, 5)array2 <- array(c(vector3, vector4), dim = c(3, 3, 2))  # create matrices and add themmatrix1 <- array1[,,2]matrix2 <- array2[,,2]result <- matrix1 + matrix2print(result)
Output:
     [,1] [,2] [,3]
[1,]    4    6    8
[2,]    4   10    5
[3,]    4   14    9

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:
# Creating Vectorsvector1 <- 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")  # taking vector as inputresult <- array(c(vector1, vector2), dim = c(3, 3, 2),                   dimnames = list(row.names, column.names, matrix.names))print(result)  # print third row of second matrixprint(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 

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:
# create two vectors and take them as input in arrayvector1 <- 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)  # using apply and calculate the sum of rows in matricesresult <- 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

Picked
R-Arrays
R Language
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
How to Change Axis Scales in R Plots?
Convert integer to string in Python
Convert string to integer in Python
Python infinity
How to set input type date in dd-mm-yyyy format using HTML ?
Matplotlib.pyplot.title() in Python