# [R-SIG-Mac] Rolling average length issue

David Winsemius dwinsemius at comcast.net
Sat Nov 6 02:44:34 CET 2010

```On Nov 5, 2010, at 12:03 PM, Sandy Zelka wrote:

>
> Please help....I'm trying to calculate a 5-point rolling average and
> I keep getting a length issue.
> My script is

Not a SIG-Mac issue.

>
> ma5<-function (x){
> y<-numeric(length(x-2)
> for(i in 2(length(x)-1)){

You seem to missing an operator between 2 and (, and I am guessing you
meant to type "*" or ":") but it is not clear SINCE YOU HAVE NOT
DESCRIBED THE PROBLEM WELL. (And you have not posted the exact error
message either.)

Also check FAQ for description of problems related to the hierarchy of
arithmetic operators in R (I don't actually remember a FAQ nubmber for
this one but it is "frequently asked":
> x <- 1:10
> 2:length(x) -1
[1] 1 2 3 4 5 6 7 8 9

> y[i]]<-(x[i-3]+x[i-2]+x[i-1]+x[i]+x[i+1]))/5

So have you checked at the ends of your ranges to see if these indices
are meaningful? What is length(x) and would it be meaningful to
request x[2-1 -3]? What is x[-2]  (or perhaps x[1-3] ) , after all? Or
x[length(x)

> }
> Y}
>
> I keep getting a replacement has length zero
> I’ve tried changing the (x-2) to -3 and -4 and still get the same
> issue.

More likely that the extremes would raise "out of range" problems than
the "interior" indices.

> I’m thinking that it should be -2 and that this issue is in the
> third line – any ideas?

Better problem description, better punctuation,  and posting to rhelp.

--
David.
>
>
> Sandy Zelka, CPA, CAE, MBA, LB
> Accounting Professor
> (518) 469-8040
>
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Mac mailing list
> R-SIG-Mac at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-mac

David Winsemius, MD
West Hartford, CT

```