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

From: Mike Dugas <>
Date: Wed, 23 Apr 2008 20:31:27 -0400

Thanks for the help. That explains why my time testing showed no difference. Is there any way to speed up the program? It is unbearably slow if I increase the number of loops.


On Wed, Apr 23, 2008 at 6:23 PM, hadley wickham <> wrote:

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

        [[alternative HTML version deleted]] mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 24 Apr 2008 - 01:05:33 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 - 02:30:32 GMT.

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

list of date sections of archive