Bird
Raised Fist0
R Programmingprogramming~20 mins

Apply functions on matrices in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Matrix Apply Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of apply() on matrix rows
What is the output of this R code that uses apply() on matrix rows?
R Programming
mat <- matrix(1:9, nrow=3, byrow=TRUE)
result <- apply(mat, 1, sum)
print(result)
A[1] 12 15 18
B[1] 6 15 24
C[1] 3 6 9
D[1] 1 2 3
Attempts:
2 left
💡 Hint
Remember, apply() with margin 1 applies the function to rows.
❓ Predict Output
intermediate
2:00remaining
Using apply() on matrix columns
What does this R code print when applying mean on matrix columns?
R Programming
mat <- matrix(c(2,4,6,8,10,12), nrow=2)
result <- apply(mat, 2, mean)
print(result)
A[1] 2 4 6
B[1] 4 6 8
C[1] 3 5 7
D[1] 5 7 9
Attempts:
2 left
💡 Hint
Margin 2 means apply function to columns.
❓ Predict Output
advanced
2:00remaining
Output of apply() with a custom function
What is the output of this R code applying a custom function to each row of a matrix?
R Programming
mat <- matrix(1:6, nrow=2)
result <- apply(mat, 1, function(x) paste(x, collapse='-'))
print(result)
A[1] "1-2-3" "4-5-6"
B[1] "1-4" "2-5"
C[1] "1-3-5" "2-4-6"
D[1] "1-2" "3-4"
Attempts:
2 left
💡 Hint
Margin 1 applies function to rows; matrix is filled column-wise by default.
🔧 Debug
advanced
2:00remaining
Identify the error in apply() usage
What error does this R code produce?
R Programming
mat <- matrix(1:9, nrow=3)
result <- apply(mat, 3, sum)
print(result)
AError in apply(mat, 3, sum) : MARGIN must be between 1 and 2
BError in apply(mat, 3, sum) : subscript out of bounds
CError in apply(mat, 3, sum) : incorrect number of dimensions
DNo error, prints sums of third dimension
Attempts:
2 left
💡 Hint
Check the dimensions of the matrix and the margin argument.
🧠 Conceptual
expert
2:00remaining
Understanding apply() output type
Given this R code, what is the type and length of result?
R Programming
mat <- matrix(c(1,2,3,4), nrow=2)
result <- apply(mat, 1, function(x) x * 2)
str(result)
A"num [1:2, 1:2]" (a 2x2 numeric matrix)
B"num [1:2]" (a numeric vector of length 2)
C"list of 2" (a list with 2 elements)
D"num [1:4]" (a numeric vector of length 4)
Attempts:
2 left
💡 Hint
When the function returns a vector, apply() simplifies to a matrix.

Practice

(1/5)
1. In R, what does the apply() function do when used on a matrix?
easy
A. It creates a new matrix with doubled values.
B. It converts the matrix into a vector.
C. It sorts the matrix elements in ascending order.
D. It applies a function to each row or column of the matrix.

Solution

  1. Step 1: Understand the purpose of apply()

    The apply() function is designed to run a function on margins of an array or matrix.
  2. Step 2: Identify what margins mean in a matrix

    Margin 1 means rows, margin 2 means columns. So apply() runs the function on each row or column.
  3. Final Answer:

    It applies a function to each row or column of the matrix. -> Option D
  4. Quick Check:

    apply() = function on rows/columns [OK]
Hint: Remember: MARGIN=1 rows, MARGIN=2 columns in apply() [OK]
Common Mistakes:
  • Thinking apply() changes matrix shape automatically
  • Confusing apply() with sorting or vector conversion
  • Using apply() without specifying margin
2. Which of the following is the correct syntax to calculate the sum of each column in a matrix m using apply() in R?
easy
A. apply(m, 2, sum)
B. apply(m, sum, 2)
C. apply(m, 1, sum)
D. apply(sum, m, 2)

Solution

  1. Step 1: Identify margin for columns

    In apply(), margin 2 means columns.
  2. Step 2: Check function placement

    The function to apply (sum) is the third argument, so syntax is apply(m, 2, sum).
  3. Final Answer:

    apply(m, 2, sum) -> Option A
  4. Quick Check:

    sum columns = apply(m, 2, sum) [OK]
Hint: Use margin=2 for columns in apply() [OK]
Common Mistakes:
  • Using margin=1 for columns instead of 2
  • Swapping function and margin arguments
  • Missing the function argument
3. What is the output of the following R code?
m <- matrix(1:6, nrow=2)
apply(m, 1, max)
medium
A. [1] 3 6
B. [1] 4 5
C. [1] 5 6
D. [1] 2 4

Solution

  1. Step 1: Understand the matrix structure

    The matrix m is created with values 1 to 6, 2 rows: Row 1: 1, 3, 5 Row 2: 2, 4, 6
  2. Step 2: Apply max function on rows (margin=1)

    Max of row 1 is 5, max of row 2 is 6.
  3. Final Answer:

    [1] 5 6 -> Option C
  4. Quick Check:

    max per row = 5, 6 [OK]
Hint: Check matrix layout before applying functions [OK]
Common Mistakes:
  • Confusing rows and columns in margin
  • Misreading matrix values
  • Assuming apply returns matrix instead of vector
4. Identify the error in this R code snippet:
m <- matrix(1:9, nrow=3)
apply(m, 3, mean)
medium
A. apply() cannot be used with mean.
B. Margin 3 is invalid for a 2D matrix.
C. Matrix m is not created properly.
D. The function mean is not defined.

Solution

  1. Step 1: Check matrix dimensions

    The matrix m has 2 dimensions (rows and columns).
  2. Step 2: Validate margin argument

    Margin 3 refers to a third dimension, which does not exist in a 2D matrix.
  3. Final Answer:

    Margin 3 is invalid for a 2D matrix. -> Option B
  4. Quick Check:

    Margin must be 1 or 2 for matrices [OK]
Hint: Use only 1 or 2 as margin for 2D matrices [OK]
Common Mistakes:
  • Using margin > 2 for 2D matrices
  • Assuming apply works on non-existent dimensions
  • Confusing apply() with array functions
5. You have a matrix m of numeric values. You want to create a new vector that contains the mean of each row but only for rows where the sum is greater than 10. Which R code correctly does this using apply()?
hard
A. apply(m, 1, function(x) if(sum(x) > 10) mean(x) else NA)[!is.na(.)]
B. apply(m, 1, function(x) if(sum(x) > 10) mean(x))
C. apply(m, 1, function(x) mean(x[sum(x) > 10]))
D. apply(m, 2, function(x) if(sum(x) > 10) mean(x))

Solution

  1. Step 1: Understand the requirement

    We want mean of each row only if that row's sum is > 10, else exclude it.
  2. Step 2: Analyze each option

    A returns NA for rows not >10 but does not remove them. C incorrectly filters inside mean. D uses margin=2 (columns) instead of rows.
  3. Step 3: Correct approach

    apply(m, 1, function(x) if(sum(x) > 10) mean(x) else NA)[!is.na(.)] returns NA for rows with sum ≤ 10, then removes NAs with [!is.na(.)], giving only desired means.
  4. Final Answer:

    apply(m, 1, function(x) if(sum(x) > 10) mean(x) else NA)[!is.na(.)] -> Option A
  5. Quick Check:

    Filter rows by sum > 10, then mean [OK]
Hint: Use if-else inside apply and remove NA after [OK]
Common Mistakes:
  • Using margin=2 instead of 1 for rows
  • Not handling rows with sum ≤ 10 properly
  • Trying to filter inside mean() incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard