Faster “for” Loops in R

We all know that the apply functions exist to get us away from relying on “for” loops (and “for” loops within “for” loops). However, I’m rather attached to using them for some particular use cases and have thus been tweaking to try and speed them up.

With a hat tip to Joseph Adler’s new book from O’Reilly, “R in a Nutshell“, I’ve found that defining the dimensions of the data object that you intend to rely on to collect the results of a loop speeds the process up dramatically!

For example, the following code takes around 23 seconds to run on my machine:
data <- NULL
for (i in 1:100000) {
x <- sample(1:100,1)
data[i] <- x*i

While this version of the code only takes 2 seconds:
data <- rep(NA,100000)
for (i in 1:100000) {
x <- sample(1:100,1)
data[i] <- x*i

The only difference is that the final object “data” has had its length predefined.

I am not ashamed to say that this discovery has made my day.


2 responses to “Faster “for” Loops in R

  1. I normally implement the former rather than the latter, just out of laziness and not thinking about it, but this certainly makes a lot of sense. For the first, R will have to keep increasing its storage for that vector, which it will do by reserving new memory and then copy the old vector into the new (bigger) space, before adding the additional data. It won’t do this for every added data value, but it will do this certainly quite a few times in that loop, and especially when the vector gets big, this is expensive, of course.

    Anyway, after reading your post, I’ll think about these things more carefully again when writing R code ;) … I just taught a whole class of students the former implementation again :)

  2. Hi Justing,

    When I first found this out I was also quite happy :)

    Notice that you can use:
    data <- numeric(100000)
    Instead of:
    data <- rep(NA,100000)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s