0
0
R Programmingprogramming~10 mins

Apply functions on matrices 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 calculate the sum of each row in the matrix.

R Programming
row_sums <- apply(matrix_data, [1], sum)
Drag options to blanks, or click blank then click option'
A1
B2
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using margin 2 sums columns instead of rows.
Using margin 0 or 3 causes errors.
2fill in blank
medium

Complete the code to calculate the mean of each column in the matrix.

R Programming
col_means <- apply(matrix_data, [1], mean)
Drag options to blanks, or click blank then click option'
A3
B1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using margin 1 sums rows instead of columns.
Using margin 0 or 3 causes errors.
3fill in blank
hard

Fix the error in the code to calculate the maximum value in each row.

R Programming
max_values <- apply(matrix_data, [1], max)
Drag options to blanks, or click blank then click option'
A1
B0
C3
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using margin 2 returns max per column instead.
Using margin 0 or 3 causes errors.
4fill in blank
hard

Fill both blanks to create a matrix of logical values where each element is TRUE if greater than 5.

R Programming
result <- apply(matrix_data, [1], function(x) x [2] 5)
Drag options to blanks, or click blank then click option'
A2
B>
C<
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using margin 2 applies function column-wise, which is not intended here.
Using < or other operators changes the logic.
5fill in blank
hard

Fill all three blanks to create a named logical vector indicating which column sums are greater than 10.

R Programming
result <- sapply(colnames(matrix_data), function([1]) sum(matrix_data[ , [2]]) [3] 10)
Drag options to blanks, or click blank then click option'
Acol
B>
C<
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > changes the condition.
Using row instead of col causes indexing errors.