What I want to ask is, algorithmically, what do the rowMeans()
and colMeans()
functions do to optimize speed?
Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?
Asked
Active
Viewed 1,602 times
4

Paul Hiemstra
- 59,984
- 12
- 142
- 149

user1482678
- 133
- 1
- 6
-
9I don't think it's algorithmic, I think it's a matter of what can be coded directly in C and what has to go through the R interpreter. – Ben Bolker Oct 06 '12 at 13:05
2 Answers
6
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.

Gavin Simpson
- 170,508
- 25
- 396
- 453
4
rowMeans
and colMeans
are faster than because they call C code directly, rather than being interpreted by the R interpreter.

Andrie
- 176,377
- 47
- 447
- 496
-
1Is there a difference in speed/efficiency between ``rowMeans`` and ``colMeans``? Thanks. (looked at various questions on so, but couldn't find it mentioned, did I miss a related post on this?) – PatrickT Oct 25 '17 at 19:00