[R] Can I get rid of this for loop using apply?
hadley wickham
h.wickham at gmail.com
Thu Apr 24 00:23:39 CEST 2008
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/
More information about the R-help
mailing list