[R] Operating on windows of data

Martin Maechler maechler at stat.math.ethz.ch
Tue Mar 23 09:40:47 CET 2004


>>>>> "Richard" == Richard A O'Keefe <ok at cs.otago.ac.nz>
>>>>>     on Tue, 23 Mar 2004 13:16:11 +1200 (NZST) writes:

    Richard> Martin Maechler <maechler at stat.math.ethz.ch> wrote
    Richard> that [using apply functions is] not [better than
    Richard> using loops], not at all.

eehm, that's not what I said (nor intended to say)!

Here's part of my post:
      Ajay> approach above works. My question is: Why is this much
      Ajay> better than writing it using loops?

	MM> it's not, not at all.
	MM> And you are very much right in all you say below!

So what I said is that 
 >> 'this' is not much better than writing it using loops

and 'this' was sapply()ing a "heavy" function
{that rather should have been stream lined instead as Brian
 Ripley pointed out clearly}.

I do advocate and use sapply() very heavily myself,
but not for calling a "large" anonymous function
because in that case the style is not really improved.

    Richard> He would be right in all he says if time efficiency
    Richard> were the only reason to prefer one coding style to
    Richard> another.

    Richard> Loop-free notatation can reduce the number of
    Richard> variables in scope, making the code easier to read
    Richard> and rearrange.  By separating "what to do with the
    Richard> elements" from "how to find the elements", it can
    Richard> lead to pieces which are separately reusable.  By
    Richard> reducing the volume of code, it can result in code
    Richard> with fewer mistakes.

    Richard> (Note the word "can" in each of those sentences.)

I completely agree with your principles here,
but *please* note again the code snippet we where talking about :

    > dat <- sapply( seq(T-width), function(i) {
    >     model <- lm(dlinrchf ~ dlusdchf + dljpychf + dldemchf, A, 
    >                 i:(i+width-1))
    >     details <- summary.lm(model)
    >     tmp <- coefficients(model)
    >     c( USD = tmp[2], JPY = tmp[3], DEM = tmp[4], 
    >            R2 = details$r.squared, RMSE = details$sigma )
    > } )
    > dat <- as.data.frame(t(dat))
    > attach(dat)

which is really an example where sapply() rather obfuscates than
clarifies.
And, BTW, when talking about code style,
attaching data frames is not good R *programming* style nowadays:
in programming, either use with(.) or access the components
directly (x$.. or x[".."] if you hate partial matching).

Martin




More information about the R-help mailing list