[R] How to?

Prof Brian D Ripley ripley at stats.ox.ac.uk
Fri Aug 25 09:52:43 CEST 2000


On Fri, 25 Aug 2000, Martin Maechler wrote:

> >>>>> "Yves" == Yves Gauvreau <cyg at sympatico.ca> writes:
> 
>       ....
> 
>     Yves> In fact what I'd like to know is what is the most efficient R way
>     Yves> to do thing like a moving average on a vector. Something generic
>     Yves> enough that one could use practically any function on a constant
>     Yves> width window of the vector?
> 
> filter()  from package ts ?

That's linear functions only.  But I'm afraid that there is no
generic fast way to do this for arbitrary functions, which is why special
cases like filter() exists. The best I can think of is along the lines of

mywinfun <- function(x, width, FUN, ...)
{
   n <- length(x) - width
   z <- substr(x, 1:n, (1:n)+width)
   sapply(z, FUN, ...)
}

which should beat the use of apply.

-- 
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