0
0
R Programmingprogramming~20 mins

Matrix creation in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matrix Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this matrix creation code?
Consider the following R code that creates a matrix. What will be the output when printed?
R Programming
m <- matrix(1:6, nrow=2, ncol=3)
print(m)
A
[,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
B
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
C
[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
D
[,1] [,2] [,3]
[1,]    6    5    4
[2,]    3    2    1
Attempts:
2 left
💡 Hint
Remember that R fills matrices by columns by default.
Predict Output
intermediate
2:00remaining
What is the value of element m[2,3] after this code?
Given the matrix creation below, what is the value stored at row 2, column 3?
R Programming
m <- matrix(seq(10, 60, by=10), nrow=3, ncol=2)
m[2,3] <- 99
print(m)
AError: subscript out of bounds
B99
C30
D60
Attempts:
2 left
💡 Hint
Check the dimensions of the matrix before assigning.
🧠 Conceptual
advanced
2:00remaining
How does the byrow argument affect matrix creation?
What is the difference in the matrix created by these two commands? m1 <- matrix(1:6, nrow=2, ncol=3) m2 <- matrix(1:6, nrow=2, ncol=3, byrow=TRUE) Choose the correct description.
Am1 fills the matrix by columns, m2 fills by rows
BBoth fill by columns, but m2 transposes the matrix
Cm1 fills the matrix by rows, m2 fills by columns
DBoth fill by rows, but m2 reverses the order
Attempts:
2 left
💡 Hint
Check the default behavior of matrix filling and what byrow=TRUE changes.
🔧 Debug
advanced
2:00remaining
What error does this matrix creation code produce?
Examine the code below and identify the error it produces when run in R: m <- matrix(1:8, nrow=3, ncol=3) print(m)
AWarning: data length is not a sub-multiple or multiple of the number of rows or columns
BError: invalid matrix dimensions
CError: number of items to replace is not a multiple of replacement length
DNo error, matrix is created with recycling of values
Attempts:
2 left
💡 Hint
Think about what happens when the data length does not match the matrix size exactly.
🚀 Application
expert
2:00remaining
How many elements are in the matrix created by this code?
What is the total number of elements in the matrix created by this R code? m <- matrix(data=rep(5, times=12), nrow=4, ncol=3)
A7
B12
C4
D3
Attempts:
2 left
💡 Hint
Multiply the number of rows by the number of columns.