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

From: Mike Dugas <mikedugas77_at_gmail.com>
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.

Mike

On Wed, Apr 23, 2008 at 6:23 PM, hadley wickham <h.wickham_at_gmail.com> wrote:

> On Wed, Apr 23, 2008 at 4:23 PM, Mike Dugas <mikedugas77@gmail.com> 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], d.call, dn.call), ...)
> 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
>
>
> --
> http://had.co.nz/
>

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive