(This article was first published on

**minimalR.com » r-bloggers**, and kindly contributed to R-bloggers)Are your *for* loops too slow in R ? Are loops that should take seconds actually taking hours ?

As I found out recently, how you structure your code can make a **huge** difference in execution times. Fortunately making a few small changes to your code can speed up these loops by several orders of magnitude.

This Stack Overflow post goes through a number of ways to optimise your *for* loops – I only implemented the first method and my loop run time went from over an hour to less than 10 seconds !!!

The secret ? to loop over a vector rather than data frames as R is optimised for vector and matrix operations.

To

**leave a comment**for the author, please follow the link and comment on their blog:**minimalR.com » r-bloggers**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...