[R] How to?

Yves Gauvreau cyg at sympatico.ca
Fri Aug 25 20:23:56 CEST 2000

> I said `something like' (that's code for `I haven't tried this'), and I
> mis-remembered what substr does. Try this:

No offence intended I didn't know your code. I'll pay more attention next

> mywinfun <- function(x, width, FUN, ...)
> {
>     n <- length(x)
>     A <- rep(x, length=width*(n+1))
>     dim(A) <- c(n+1, width)
>     sapply(split(A, row(A)), FUN, ...)[1:(n-width)]
> }
> It's more efficient than embed, and more efficient that apply(A, 1,...)
> in this case.  But there are limits to how efficient interpreted R code
> gets in such problems.

I understand that interpreted code can and is in many cases a lot slower
then machine code especially in loop cases. This is why, if I understand
correctly, documentation for writing R code and any other interpreted
language for that matter strongly suggest to vectorize operations. In such
case the relative penalty for interpreting and passing data to the machine
code (internally) decreases as the data size increases.

I'm curious to know the reasons why sapply is or would be faster then apply
in general?

I'm also curious to know a bit more on the following. Suppose I (can) write
a function (as DLL in my case) that prepares the vector for the FUN as in
one of the ?apply function. Would the gain in speed be worthwhile assuming
that I can call it from my DLL function of course?

Second, assuming I can call R internal functions from my own this would mean
no interpretation and thus minimal overhead. I assume that in those cases
the speed improvement would be quite important. It would be a fairly complex
task for me to create such an external function and I'd like to know before
hand if it's worth the trouble I would get into?


Yves Gauvreau

> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272860 (secr)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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