do until loop in R -
at end of wits, sorry wrong place, or done incorrectly. first time asking here. new r, little programming experience (a pascal class in college, , @ macromedia lingo way - so, not afraid of code).
to keep things short , simple, think best show have, , like. have spent hours upon hours searching , trying solution.
an example of have (it xts object called "signals", , indexed days (left out here make example simple):
open close position 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
and happen:
open close position 0 0 0 1 0 1 0 0 1 0 0 1 0 1 1 0 0 0
basically, when "open" true, repeat 1s in "position" until "close" true. amazingly simple, think, somehow can't make work. here 1 example of got thought maybe close, gets stuck in endless loop:
for (i in 1:nrow(signals)) { if (signals[i,"open"]==1) next while (signals[i,"close"] == 0) { signals[i,"position"] <- 1 } }
thank you!
edit - left out important qualifier. there times first true statement in "close" come before first true statement in "open." however, wrote out here, suppose easier 'clean' close column somehow, there no 1s in prior point of first 1 in open column.
however, if has idea how all, feel free add additional information. thanks!
you don't have use loops this:
open <- c(0,1,0,0,0,0) close <- c(0,0,0,0,1,0) position <- cumsum(open-close) position [1] 0 1 1 1 0 0
note closes immediately, if want on line after close signal, use:
cumsum(open-c(0,close[-length(close)])) [1] 0 1 1 1 1 0
the reason while
statment never ends have nothing modify being tested, i
doesn't incremented.
Comments
Post a Comment