[R] How to?

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


On Fri, 25 Aug 2000, Yves Gauvreau wrote:

> I tried the function you proposed and I get an error:
> 
> Error in substr(x, as.integer(start), as.integer(stop)) :
>         invalid argument in substr()
> 
> I must admit I don't understand the use of substr() in this case, x being a
> numerical vector. I understand that your using it to provide FUN with its
> argument i.e. the window of data. I'm using R1.1 on NT does this mean it
> could work width with the newer version?

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

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.

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