w3resource

R Programming: Create an 3 dimensional array of 24 elements using the dim() function

R Programming: Array Exercise-3 with Solution

Write a R program to create an 3 dimensional array of 24 elements using the dim() function.

Sample Solution :

R Programming Code :

v =  sample(1:5,24,replace = TRUE)
dim(v) = c(3,2,4)
print("3-dimension array:")
print(v)

Sample Output:

[1] "3-dimension array:"
, , 1

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

, , 2

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

, , 3

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

, , 4

     [,1] [,2]
[1,]    3    3
[2,]    1    5
[3,]    1    2                         

R Programming Code Editor:



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

Previous: Write a R program to create an array of two 3x3 matrices each with 3 rows and 3 columns from two given two vectors.
Next: Write a R program to create an array of two 3x3 matrices each with 3 rows and 3 columns from two given two vectors. Print the second row of the second matrix of the array and the element in the 3rd row and 3rd column of the 1st matrix.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.