With R. For a sequence of numbers x, how do I best create a function S(x, r, s) that computes sum(x[t]*x[t+r-s], t ranges from s to length(x)-r+1; r,s>0. Thanks.
+3
A:
You might want to enforce a relationship between the length of x, and the values of s and r as well, or you can end up with some strange results. Try 6:1 for instance.
sumfunc <- function(x, s, r) {
s <- s[s>0]
r <- r[r>0]
if(!(length(r)==1 && length(s)==1)) stop("s and r should be numbers > 0")
t <- s:(length(x)-r+1)
return(sum(x[t]*x[t+r-s]))
}
Shane
2009-12-22 09:58:30
Thanks, Shane. If not by looking at what Masters are doing, how else I know what's best.
knot
2009-12-24 00:22:49
Will best be better when modified as return(sum(as.numeric((x[t]*x[t+r-s])))) ?
knot
2010-01-08 07:15:39