[R] How to?

Yves Gauvreau cyg at sympatico.ca
Fri Aug 25 13:40:56 CEST 2000


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?

Also I came across the function embed() also in (ts) which could be used to
create a matrix like so:

embed(1:10,3)
     [,1] [,2] [,3]
[1,]    3    2    1
[2,]    4    3    2
[3,]    5    4    3
[4,]    6    5    4
[5,]    7    6    5
[6,]    8    7    6
[7,]    9    8    7
[8,]   10    9    8

then one could use apply(embed(1:10,3), 1, FUN) which does the job but again
it's relatively slow for large dataset. So it seem that I'll either have to
wait for someone to write a faster version of apply or be patient and since
I can't write my own C function then I'll have to be patient.

Thanks to all, R is great software.

YG


----- Original Message -----
From: "Prof Brian D Ripley" <ripley at stats.ox.ac.uk>
To: "Martin Maechler" <maechler at stat.math.ethz.ch>
Cc: "Yves Gauvreau" <cyg at sympatico.ca>; <r-help at stat.math.ethz.ch>
Sent: Friday, August 25, 2000 3:52 AM
Subject: Re: [R] How to?


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

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