# apply vs for

**The stupidest thing... » R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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[i] <- max(x[i,]) + }) user system elapsed 3.719 0.446 4.164 > system.time(mx2 <- apply(x, 1, max)) user system elapsed 5.548 1.783 7.333

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.”

**leave a comment**for the author, please follow the link and comment on their blog:

**The stupidest thing... » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.