What I want to ask is, algorithmically, what do the rowMeans()
and colMeans()
functions do to optimize speed?
In addition, consider what lapply()
does. It sets up repeated calls to the function mean()
. So as well as the overhead of actually computing a mean (which is done in fast C code), the lapply()
version repeatedly incurs the overhead of the sanity checking code and method dispatch associated with mean()
.
rowMeans()
and colMeans()
incur only a single set of sanity checks as internally, their C code is optimised to loop over the rows/columns there rather than via separate R calls.
rowMeans
and colMeans
are faster than because they call C code directly, rather than being interpreted by the R interpreter.