w3resource

Create a 3D array in R Using Given Columns, Rows, and Tables

R Programming: Array Exercise-6 with Solution

Write a R program to create an array using four given columns,three given rows,and two given tables and display the content of the array.

Sample Solution :

R Programming Code :

# Create a 3-dimensional array with values from 1 to 30
# The array has dimensions 3 rows, 4 columns, and 2 matrices (tables)
array1 = array(1:30, dim=c(3,4,2))

# Print the content of the 3-dimensional array to the console
print(array1)

Output:

, , 1

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

, , 2

     [,1] [,2] [,3] [,4]
[1,]   13   16   19   22
[2,]   14   17   20   23
[3,]   15   18   21   24                        

Explanation:

  • array1 = array(1:30, dim=c(3,4,2)): Creates a 3-dimensional array named array1.
    • 1:30: Provides the data values to fill the array (numbers from 1 to 30).
    • dim=c(3,5,2): Specifies the dimensions of the array (3 rows, 4 columns, and 2 tables/layers).
  • print(array1): Displays the content of array1 in the console.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to combine three arrays so that the first row of the first array is followed by the first row of the second array and then first row of the third array.
Next: Write a R program to create a two-dimensional 5×3 array of sequence of even integers greater than 50.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/array/r-programming-array-exercise-6.php