Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a 2x2 matrix with values 1 to 4.
R Programming
mat <- matrix(1:4, nrow = [1], ncol = 2)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number of rows like 3 or 4.
Confusing rows and columns.
✗ Incorrect
The matrix has 2 rows and 2 columns, so nrow = 2 is correct.
2fill in blank
mediumComplete the code to add two matrices A and B.
R Programming
result <- [1] + B Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
sum which sums all elements instead of element-wise addition.Using
matrix keyword incorrectly.✗ Incorrect
To add two matrices, use the + operator between them. Here, A + B.
3fill in blank
hardFix the error in the code to multiply matrices X and Y.
R Programming
product <- X [1] Y Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
* which does element-wise multiplication.Using modulo operators like
%% or %.✗ Incorrect
Matrix multiplication in R uses the operator %*%, not *.
4fill in blank
hardFill both blanks to create a matrix M with 3 rows and 4 columns filled by row.
R Programming
M <- matrix(1:12, nrow = [1], ncol = [2], byrow = TRUE)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping rows and columns.
Setting
byrow to FALSE by mistake.✗ Incorrect
The matrix has 3 rows and 4 columns, so nrow = 3 and ncol = 4.
5fill in blank
hardFill all three blanks to create a named matrix mat with row and column names.
R Programming
mat <- matrix(1:6, nrow = [1], ncol = [2], dimnames = list([3], c("A", "B")))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching row names length with number of rows.
Swapping rows and columns counts.
✗ Incorrect
The matrix has 3 rows and 2 columns, so nrow = 3 and ncol = 2. The row names vector must match the number of rows, so c("Row1", "Row2", "Row3") is correct.