R Program to create an array using given four columns, three rows, and two tables


March 26, 2022, Learn eTutorial
1233

How to create an array using given four columns, three  rows, and two tables

Here we explain how to write an R program to create an array using four columns, three rows, and two tables. Here we are using a built-in functions array(),dim() for this. The dim() function helps retrieve or set the dimension of an object and the array() function helps create the array. The syntax of this function is 

dim(x) #Where x is an R object like a matrix, array, or data frame
array(data = NA, dim = length(data), dimnames = NULL)  #Where data is a vector giving data to fill the array

How to create an array using given four columns, three  rows, and two tables in the R program

Below are the steps used in the R program to create an array using given four columns, three rows, and two tables. In this R program, we give the values directly to an array(),dim(). Here we are using variable for holding the result array. Here we are creating an array using the given data. Finally, print the array.

ALGORITHM

STEP 1: Assign array elements into the variable A

STEP 2: The array is created by using array(1:30, dim=c(3,5,2))

STEP 3: Print the array

R Source Code

                                          A =  array(1:30, dim=c(3,5,2))
print(A)
                                      

OUTPUT

, , 1

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]   16   19   22   25   28
[2,]   17   20   23   26   29
[3,]   18   21   24   27   30