w3resource

R Programming: Create an array of two 3x3 matrices each with 3 rows and 3 columns from two given two vectors

R Programming: Array Exercise-2 with Solution

Write a R program to create an array of two 3x3 matrices each with 3 rows and 3 columns from two given two vectors.

Sample Solution :

R Programming Code :

print("Two vectors of different lengths:")
v1 =  c(1,3,4,5)
v2 =  c(10,11,12,13,14,15)
print(v1)
print(v2)
result = array(c(v1,v2),dim = c(3,3,2))
print("New array:")
print(result)

Sample Output:

[1] "Two vectors of different lengths:"
[1] 1 3 4 5
[1] 10 11 12 13 14 15
[1] "New array:"
, , 1

     [,1] [,2] [,3]
[1,]    1    5   12
[2,]    3   10   13
[3,]    4   11   14

, , 2

     [,1] [,2] [,3]
[1,]   15    4   11
[2,]    1    5   12
[3,]    3   10   13                         

R Programming Code Editor:



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

Previous: Write a R program to convert a given matrix to a 1 dimensional array.
Next: Write a R program to create an 3 dimensional array of 24 elements using the dim() function.

Test your Python skills with w3resource's quiz

What is the difficulty level of this exercise?