Re: [R] Can I get rid of this for loop using apply?

From: hadley wickham <>
Date: Wed, 23 Apr 2008 17:23:39 -0500

On Wed, Apr 23, 2008 at 4:23 PM, Mike Dugas <> wrote:
> The answer to my post is yes (which I just figured out).

Switching from for to apply isn't going to speed up your code. If you carefully read the source code of apply, you'll see the guts of the work is done by:

 for (i in 1:d2) {

        tmp <- FUN(array(newX[, i],,, ...)
        if (!is.null(tmp))
            ans[[i]] <- tmp


i.e apply uses for internally. The reason to use apply instead of a for loop is so that you can better express the intent of your algorithm.



______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 23 Apr 2008 - 22:29:50 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Apr 2008 - 01:30:33 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive