How to create an Array 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.

Example:

# R program to illustrate an array
 
A = array(
    # Taking sequence of elements
    c(1, 2, 3, 4, 5, 6, 7, 8),
 
    # Creating two rectangular matrices
    # each with two rows and two columns
    dim = c(2, 2, 2)                       
)
 
print(A)

Output:

, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8