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}
system.time(
for (i in 1:10000) {
data[i,2]=calculation()
data[i,3]=somethingElse()
}
)
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).