apply vs for
R-bloggers 2013-04-03
It’s widely understood that, in R programming, one should avoid for loops and always try to use apply-type functions.
But this isn’t entirely true. It may have been true for Splus, back in the day: As I recall, that had to do with the entire environment from each iteration being retained in memory.
Here’s a simple example:
> x <- matrix(rnorm(4000*40000), ncol=4000)> system.time({+ mx <- rep(NA, nrow(x))+ for(i in 1:nrow(x)) mx <- max(x[i,])+ }) user system elapsed 3.298 0.372 3.668 > system.time(mx2 <- apply(x, 1, max)) user system elapsed 4.173 0.698 4.870 There’s a great commentary on this point by Uwe Ligges and John Fox in the May, 2008, issue of R News (see the “R help desk”, starting on page 46, and note that R News is now the R Journal).
Also see the related discussion at stackoverflow.
They say that apply can be more readable. It can certainly be more compact, but I usually find a for loop to be more readable, perhaps because I’m a C programmer first and an R programmer second.
A key point, from Ligges and Fox: “Initialize new objects to full length before the loop, rather than increasing their size within the loop.”
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...