Moving average of all sorts are supported in package TTR,
http://cran.r-project.org/web/packages/TTR/index.html.
For arithmetic moving averages, you can also use cumsum followed by a diff
with the correct lag
Also for more general problems that involve rolling time windows, you should
consider rollapply in
package zoo http://cran.r-project.org/web/packages/zoo/index.html

On Wed, Nov 10, 2010 at 7:52 PM, Santosh Srinivas <
santosh.srinivas@gmail.com> wrote:

> Hi Stephen,
>
> Can't understand your question without a dataset (see dput) and
> reproducible
> sample
> But not sure why sum(x[n:(n-20),]) would not work (see ?unlist too for
> possible solutions... from experience)
>
> HTH
>
> -----Original Message-----
> From: r-sig-finance-bounces@stat.math.ethz.ch
> [mailto:r-sig-finance-bounces@stat.math.ethz.ch] On Behalf Of Stephen
> Choularton
> Sent: 11 November 2010 08:58
> To: r-sig-finance@stat.math.ethz.ch
> Subject: [R-SIG-Finance] picking up the current index plus n -1, n-2, ...,
> n-20
>
> Hi
>
> I am trying to use
>
> apply(X=x,MARGIN=1,FUN=myFunction)
>
> to pick up and calculate moving averages and the like.
>
> I can get this type of thing to work:
>
> myFunction <- function (x){return( 2 * x)}
>
> but of course that only work on one index at a time when I want to do
> something like this:
>
> myFunction <- function (x){return( sum(x[n,]:x[n-20]))}
>
> but of course it doesn't work.
>
> I am still looking in R in a Nutshell but I just wondered if someone
> could put me out of my misery.
>
> --
> Stephen Choularton Ph.D., FIoD
>
> _______________________________________________
> R-SIG-Finance@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>
> _______________________________________________
> R-SIG-Finance@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>

	[[alternative HTML version deleted]]

