0
0
R Programmingprogramming~10 mins

Apply functions on matrices in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Apply functions on matrices
Start with Matrix
Choose function to apply
Select margin: rows or columns
Apply function to each row/column
Return result vector or matrix
We start with a matrix, pick a function, decide to apply it by rows or columns, then get the result.
Execution Sample
R Programming
mat <- matrix(1:6, nrow=2)
apply(mat, 1, sum)
apply(mat, 2, mean)
This code creates a 2x3 matrix, sums each row, then finds the mean of each column.
Execution Table
StepActionMatrix StateFunction AppliedResult
1Create matrix mat[[1,3,5],[2,4,6]]NoneMatrix created
2Apply sum by rows (margin=1)[[1,3,5],[2,4,6]]sum[9, 12] (row sums)
3Apply mean by columns (margin=2)[[1,3,5],[2,4,6]]mean[1.5, 3.5, 5.5] (column means)
4EndSame matrixNoneFinished applying functions
💡 All rows and columns processed, function applied as requested
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
matNULL[[1,3,5],[2,4,6]][[1,3,5],[2,4,6]][[1,3,5],[2,4,6]][[1,3,5],[2,4,6]]
result_row_sumNULLNULL[9, 12][9, 12][9, 12]
result_col_meanNULLNULLNULL[1.5, 3.5, 5.5][1.5, 3.5, 5.5]
Key Moments - 3 Insights
Why does apply(mat, 1, sum) return a vector of length 2?
Because margin=1 means apply the function to each row. The matrix has 2 rows, so sum is calculated twice, once per row (see execution_table step 2).
What does margin=2 mean in apply(mat, 2, mean)?
Margin=2 means apply the function to each column. The matrix has 3 columns, so mean is calculated for each column separately (see execution_table step 3).
Does the original matrix change after apply?
No, apply does not modify the original matrix. It returns a new vector or matrix with results. The original matrix stays the same (see variable_tracker for 'mat').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the sum of the first row?
A12
B6
C9
D3
💡 Hint
Check the 'Result' column at step 2 in execution_table for row sums.
At which step does the mean function get applied to columns?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Function Applied' column in execution_table to find when 'mean' is applied.
If we change margin=1 to margin=2 in apply(mat, 1, sum), what changes in the result?
ASum is calculated for each row instead of columns
BSum is calculated for each column instead of rows
CResult becomes a matrix instead of a vector
DNo change in result
💡 Hint
Refer to key_moments about margin meaning and execution_table steps 2 and 3.
Concept Snapshot
apply(matrix, margin, function)
margin=1 applies function to rows
margin=2 applies function to columns
Returns vector or matrix of results
Original matrix stays unchanged
Full Transcript
This visual trace shows how to use the apply function on matrices in R. We start by creating a matrix named mat with 2 rows and 3 columns. Then, we apply the sum function to each row by setting margin=1, which returns a vector of row sums. Next, we apply the mean function to each column by setting margin=2, which returns a vector of column means. The original matrix remains unchanged throughout. The key points are that margin=1 means rows, margin=2 means columns, and apply returns new results without modifying the original matrix.