[R] problem with do.call

Douglas Bates bates at stat.wisc.edu
Thu Apr 11 17:27:22 CEST 2002

Ernesto Jardim <ernesto at ipimar.pt> writes:

> Hi
> This was not my understanding. I thougth that if you can use functions
> like apply and similar instead of for loops your code will be faster.
> Basicly relying on these functions code which is (should be) optimized
> for speed.
> If what you're saying is true then using functions like apply is a
> matter of simplicity and not speeding up the code. 
> Is this correct ?


If you examine the apply function you will see that the bulk of the
work is done in a loop

    if (length(d.call) < 2) {
        if (length(dn.call)) 
            dimnames(newX) <- c(dn.call, list(NULL))
        for (i in 1:d2) ans[[i]] <- FUN(newX[, i], ...)
    else for (i in 1:d2) ans[[i]] <- FUN(array(newX[, i], d.call, 
        dn.call), ...)

In their book "S Programming" (Springer, 2000) Venables and Ripley
discuss general strategies for writing R functions and for making them
faster.  One general principle is to profile the code before
implementing changes.  The manual "Writing R Extensions" has a section
on "Profiling R code" which is highly recommended.
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list