Completion requirements
These exercises test your knowledge of creating, accessing, and manipulating arrays and matrices. These exercises do not count toward your grade. They are just for practice!
Challenge 1
Write a R program to create a matrix taking a given vector of numbers as input and define the column and row names. Display the matrix.
Sample Output 1
[1] "Original Matrix:"
col1 col2 col3 col4
row1 1 2 3 4
row2 5 6 7 8
row3 9 10 11 12
row4 13 14 15 16
Challenge 2
Write an R program to convert a matrix to a 1-dimensional array.
Sample Output 2
[1] "Original Matrix:"
col1 col2 col3 col4
row1 1 2 3 4
row2 5 6 7 8
row3 9 10 11 12
row4 13 14 15 16
[1] "1 dimensional array (column wise):"
[1] 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
[1] "1 dimensional array (row wise):"
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Challenge 3
Write a R program to access the element at 3rd column and 2nd row, only the 3rd row and only the 4th column of a given matrix.
Sample Output 3
[1] "Original Matrix:"
col1 col2 col3 col4
row1 1 2 3 4
row2 5 6 7 8
row3 9 10 11 12
row4 13 14 15 16
[1] "Access the element at 3rd column and 2nd row:"
[1] 7
[1] "Access only the 3rd row:"
col1 col2 col3 col4
9 10 11 12
[1] "Access only the 4th column:"
row1 row2 row3 row4
4 8 12 16
Source: w3resource, https://www.w3resource.com/r-programming-exercises/matrix/index.php
This work is licensed under a Creative Commons Attribution 4.0 International License.
Last modified: Tuesday, 3 January 2023, 5:21 PM