Take the 2-minute tour ×

This question already has an answer here:

In R, I create a data frame in a following way:

data <- data.frame(dummy=rep('dummy',10000))
data$number = 0
data$another = 1

When I run a for loop that assigns values to data frame (iterating through rows), my code runs infinitely slow

calculation <- function() {2}
somethingElse <- function() {3}

 for (i in 1:10000) {

The above snippet runs in 20 seconds on my laptop. In other languages like C or Java, this finishes instantly. Why is it so slow in R? I remember reading that R stores matrices column by column (unlike C, for example, where it's row by row). But still, I'm puzzled about why it takes so much time. Shouldn't my data.frame fit comfortably in memory (eluding slow disk write behavior)?

As a continuation of my question, I'd like to ask for a quick way to fill my data frame by row, if there exists one.

EDIT: Please note that I'm not trying to assign constants 2 and 3 to my data frame, in the actual problem that I was trying to solve calculation() and somethingElse() are a bit more complicated and depend on another data frame. My question is about efficient insertion into data frame in a loop (and I'm also curious about why this is so slow).

share|improve this question
If you are unable to provide an example that actually matches your situation, no one will be able to help you. –  joran May 23 '13 at 20:38
In the first snippet, I do initialize the data frame. If you do a str(data) after the first snippet, it is "10000 obs. of 3 variables". –  Davor May 23 '13 at 20:42
Look, the short answer is that R isn't C, and so techniques that are fast in C may be slow in R. If I had to guess, the real solution to your problem would be to totally rethink how you're calculating the values being inserted. But we obviously can't help with that, because you've provided us no information on that topic. –  joran May 23 '13 at 20:45
@joran I'm quite clear that R is not C. If you know why this is so slow, please elaborate in more detail. What kind of argument copying is taking place here? Why is reading from data frame in the same way fast, but writing is not (even though data frame is pre-allocated)? –  Davor May 23 '13 at 21:09
show 3 more comments

marked as duplicate by Ari B. Friedman, joran, mnel, Vishal, Freelancer May 24 '13 at 5:06


1 Answer

The answer is vectorization:

data[,2] = 2
data[,3] = 3

finishes instantly for me. For loops in interpreted languages like R are veeeeery slow. Performing this kind of operation by assigning a vector directly (i.e. vectorized) is much, much faster.

Programming in a new language requires a new mindset. Your approach breathes a compiled language, no need for the for loop.

share|improve this answer
In my actual problem, I assign to this data frame something that I've calculated (from some other data). I know that I can assign all the same values using what you wrote above, but my values all different and I need to assign them one by one. –  Davor May 23 '13 at 20:33
The for loop isn't slow, it's the copying induced by the assignment. –  joran May 23 '13 at 20:35
@Davor if you want an accurate answer, please extend your example above. This answers the question you ask above, although this is not your real question. –  Paul Hiemstra May 23 '13 at 20:39
I've edited my question so that it's more clear about what I'm actually asking. –  Davor May 23 '13 at 20:52
As long as the result of calculation is vector of the length of data[,2], this will still work fine. –  Paul Hiemstra May 23 '13 at 20:54
show 2 more comments
