Skip to content
Prev 58980 / 398502 Next

unavoidable loop? a better way??

On Saturday 13 November 2004 00:51, James Muller wrote:
Does this work at all? I get
+    x[1] <- p[1]
+    for (i in c(2:length(p))) {
+      x[i] <- 0.8*p[i] + 0.2*p[i-1]
+    }
+    return (x)
+ }
Error in myfunc(1:10) : Object "x" not found


Anyway, simple loops are almost always avoidable. e.g.,

myfunc <- function (p) {
   x <- p
   x[-1] <- 0.8 * p[-1] + 0.2 * p[-length(p)]
   x
}

Deepayan