The usage from the R Documenation is as follows:
- X is an array or matrix;
- MARGIN is a variable that determines whether the function is applied over rows (MARGIN=1), columns (MARGIN=2), or both (MARGIN=c(1,2));
- FUN is the function to be applied.
An illustrative example
Consider the code below:
# Create the matrix
# Return the product of each of the rows
# Return the sum of each of the columns
# Return a new matrix whose entries are those of 'm' modulo 10
In the last example, we apply a custom function to every entry of the matrix. Without this functionality, we would be at something of a disadvantage using R versus that old stalwart of the analyst: Excel. But with the apply function we can edit every entry of a data frame with a single line command. No autofilling, no wasted CPU cycles.
In the next edition of this blog, I will return to looking at R’s plotting capabilities with a focus on the ggplot2 package. In the meantime, enjoy using the apply function and all it has to offer.