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

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

On Wed, Apr 23, 2008 at 4:23 PM, Mike Dugas <mikedugas77_at_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/

______________________________________________
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 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive