0
0
R Programmingprogramming~10 mins

Matrix arithmetic in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A1
B3
C2
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number of rows like 3 or 4.
Confusing rows and columns.
2fill in blank
medium

Complete the code to add two matrices A and B.

R Programming
result <- [1] + B
Drag options to blanks, or click blank then click option'
AA
BB
Cmatrix
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum which sums all elements instead of element-wise addition.
Using matrix keyword incorrectly.
3fill in blank
hard

Fix 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'
A*
B%*%
C%
D%%
Attempts:
3 left
💡 Hint
Common Mistakes
Using * which does element-wise multiplication.
Using modulo operators like %% or %.
4fill in blank
hard

Fill 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'
A3
B4
CFALSE
DTRUE
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping rows and columns.
Setting byrow to FALSE by mistake.
5fill in blank
hard

Fill 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'
A2
B3
Cc("Row1", "Row2", "Row3")
Dc("Row1", "Row2")
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching row names length with number of rows.
Swapping rows and columns counts.