- 3 Minutes Wednesdays
- Posts
- How to speed up your R code
How to speed up your R code
Howdy!
Many greetings from Ulm, Germany! You may be surprised that my newsletter is already back this week. I figured, if I'm writing shorter, bite-sized newsletters, then I may as well deliver them every week. So let's dive in.
How to speed up your R code
Everyone can agree that slow code sucks. Unfortunately, R is somewhat slow compared to lightning-fast programming languages like C++. Of course, R has other features that make up for its slowness (to some degree). Still, sometimes you need more speed than what R can offer.
Fortunately, there are packages that help you speed up your code without too much hassle. Today, I'm going to introduce you to two such packages. Say hello to {collapse} and {Rcpp}.
The {collapse} package
{collapse} is truly ingenious. If you're using {dplyr} verbs like group_by() or summarise(), then you're in luck. {collapse} has many counterparts that work exactly the same but faster. For example, there is fgroup_by() or fsummarise().
Additionally, {collapse} comes with faster implementations of popular summary functions, e. g. fmean() or fvar(). Here's a small benchmark that compares {dplyr} and {collapse}.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/640e0a18-c635-4bcc-8a64-f0b4d6b53a42/2022_09_04_20_34_02.705719.png)
For more benchmarks, you can check out my corresponding Twitter thread. Fun fact: Even the creator of {collapse} chimed in and delivered more insights.
The {Rcpp} package
As I said, R can be slow. Most of the time, though, not everything is slow. Often, the culprit is a single function. And let's be honest: We've likely built this slow function ourselves.
Thus, it would be great, if we can just take that slow function and implement it in a faster language, say, C++. That's the purpose of {Rcpp}. It helps you to write a function in C++ and makes it available in R.
Yeah, I know what you're thinking. "I don't know jack about C++. How am I supposed to write faster code like that?" To be fair, I used to think that too. But you don't need to "know" C++ to write one function. Most of the time, what you need is a five-minute, bare minimum crash course and a little bit of patience.
The former I can give you. You're on your own with the latter. But let me remind you that a little bit of patience with C++ can pay off big time. Anyway, come and get your C++ crash course in one of my Twitter threads.
Resources
Advanced R by Hadley Wickham: This book helped me dive into {Rcpp}. Other than {Rcpp}, the book offers many insights into the inner workings of R. So it's a good fit if you like technical details. It's available for free online. Alternatively, you can buy it on Amazon.
That's a wrap! As always, I'm happy to hear your feedback about this week's issue. Feel free to write me on Twitter or reply to this mail.
Enjoy the rest of your day!Albert
Reply