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
Step 1: Identify margin for columns
In apply(), margin 2 means columns.
Step 2: Check function placement
The function to apply (sum) is the third argument, so syntax is apply(m, 2, sum).
Final Answer:
apply(m, 2, sum) -> Option A
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
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
Step 2: Apply max function on rows (margin=1)
Max of row 1 is 5, max of row 2 is 6.
Final Answer:
[1] 5 6 -> Option C
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
Step 1: Check matrix dimensions
The matrix m has 2 dimensions (rows and columns).
Step 2: Validate margin argument
Margin 3 refers to a third dimension, which does not exist in a 2D matrix.
Final Answer:
Margin 3 is invalid for a 2D matrix. -> Option B
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
Step 1: Understand the requirement
We want mean of each row only if that row's sum is > 10, else exclude it.
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.
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.