I have a big performance problem in R. I wrote a function that iterates over a `data.frame` object. It simply adds a new column to a `data.frame` and accumulates something. (simple operation). The `data.frame` has roughly 850K rows. My PC is still working (about 10h now) and I have no idea about the runtime. dayloop2 <- function(temp){ for (i in 1:nrow(temp)){ temp[i,10] <- i if (i > 1) { if ((temp[i,6] == temp[i-1,6]) & (temp[i,3] == temp[i-1,3])) { temp[i,10] <- temp[i,9] + temp[i-1,10] } else { temp[i,10] <- temp[i,9] } } else { temp[i,10] <- temp[i,9] } } names(temp)[names(temp) == "V10"] <- "Kumm." return(temp) }Any ideas how to speed up this operation?
Loop is very slow; in each itteration, the data.frame gets rebuilt. There is always 99% a vector-based solution. The best way is to use a vector-based solution or use one of the apply functions. I can help you more if you provide more details.