Dear expeRts,
recently I asked for a nice way to re-program a problem
without using control constructs such as "for" or
"sapply(1:length(x), ...". Is there a way to program
a random walk with a reflecting boundary without resorting
to such constructs? A working solution is

ranwalk <- function(length, bound) {
    k <- cumsum(sample(c(-1, 1), length, replace=TRUE))
    while( any(abs(k) > bound) ) {
        ri <- min(which(abs(k) > bound))
        k[ri:length] <- k[ri:length] - 2 * sign(k[ri])
    }
    k
}

but it uses "while" and has the same expression in the
"while" statement and the following line. Is there
a sensible way to reprogram it using the "whole object"
approach?

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to