[R] Vectorize a bootstrapped calculation

Fernando Saldanha fsaldan1 at gmail.com
Sat May 7 20:18:48 CEST 2005

I would like to vectorize a calculation that is bootstrapped, in the
sense that each step uses the results of the previous steps. For

x <- rep(NA, 5)
y <- rnorm(5)
for (i in 1:5) {
	x[i] <- max(y[i], ifelse(i > 0, min(x[1:i-1], 0)))

(the functions max and min are used just as an example, I would like
to do something like this for arbitrary functions f and g).

It would be nice to be able to write something like:

x <- cumsapply(y, max, min)

where the first function, max in this example, would be applied to the
i-th element of y, and the second function, min in the example, would
be applied to the vector x[1:i-1].

Of course one can program such a function, the issue is can it be done
efficiently, and not just as a wrapping of the for loop above?


More information about the R-help mailing list