w3resource

R Programming: Create an array, passing in a vector of values and a vector of dimensions, also provide names for each dimension

R Programming: Basic Exercise-17 with Solution

Write a R program to create an array, passing in a vector of values and a vector of dimensions. Also  provide names for each dimension.

Sample Solution :

R Programming Code :

a =  array(
  6:30,
  dim = c(4, 3, 2),
  dimnames = list(
    c("Col1", "Col2", "Col3", "Col4"),
    c("Row1", "Row2", "Row3"),
    c("Part1", "Part2")
  )
)
print(a)

Sample Output:

, , Part1

     Row1 Row2 Row3
Col1    6   10   14
Col2    7   11   15
Col3    8   12   16
Col4    9   13   17

, , Part2

     Row1 Row2 Row3
Col1   18   22   26
Col2   19   23   27
Col3   20   24   28
Col4   21   25   29                         

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 a 5 × 4 matrix , 3 × 3 matrix with labels and fill the matrix by rows and 2 × 2 matrix with labels and fill the matrix by columns.
Next: Write a R program to create an array with three columns, three rows, and two "tables", taking two vectors as input to the array. Print the array.

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.