Are R loops really that slow?

Alwin Wang
6 min readMay 1, 2022

For-loops are not idiomatic R code” is something I have heard many times. In trivial cases or fast prototyping, for-loops are fantastic yet I have always converted them into base::apply functions, or pbapply::pbapply for progress bars or even parallel::parApply for parallel computation afterwards.

However, I honestly hadn’t looked into why R loops are slow before. The top result on Google suggests that “Loops are slower in R than in C++ because R is an interpreted language (not compiled)” but I wanted to take an in-depth look. I hoped understand if apply does anything special under the hood or if it is syntactic sugar similar to my analogy of $ signs in Excel formulas.

Benchmarking of different ways to calculate the mean of grouped data
Snapshot of results: Benchmarking of different ways to calculate the mean of grouped data. Image by author.

Vectorisation

My first thought was that apply must be doing something special, such as vectorisation. To my surprise, apply is just a fancy wrapper for a loop with smart pre-allocation! So my next challenge was to try write a loop as fast as apply using pre-allocated memory for results.

--

--

Alwin Wang

Scientific computing enthusiast, aerospace engineer, volunteer honorary data scientist. Support me here: https://medium.com/@alwinw/membership