Generating time series for generator ON or OFF Want to create time series for the nuclear power plant performance using the equation for Time to fail and time to repair for 8736 hours in the year so that I have the time series in hours when the generator is operating in when is not. The starting condition is that the generator is operating on the first hour. 

For sure there is a more elegant solution for simulating this I'm just not able to find it. Any comment or help will be appreciated. The code is working but I think there is a better solution

TTF&lt;-2940 # MEDIUM TIME TO FAIL(hours)
MTTR&lt;-60 # MEDIUM TIME TO REPAIR (hours)
TTF&lt;--MTTF*log(runif(100))# equation for Time to fail 
TTR&lt;--MTTR*log(runif(100))# equation for Time to repair
mix&lt;-rep(0,length(TTF)+length(TTR))
sw&lt;-rep(0,length(TTF)+length(TTR))
for(i in 1:length(TTF)){
mix[2*i-1]&lt;-TTF[i]
sw[2*i-1]&lt;-1
mix[2*i]&lt;-TTR[i]
}
cmix&lt;-cumsum(mix)
ccmix&lt;-cbind(cmix[1:which(cmix&gt;8736)],sw[1:which(cmix&gt;8736)])
ccmix[dim(ccmix)[1],1]&lt;-8736


G1&lt;-round(ccmix)
# transform binary values
G1[,2][G1[,2] == 1] &lt;- 400 # 400 MW is the capacity of power plant

G1 &lt;- cbind(G1, c(G1[1,1], diff(G1[,1])))
a1 &lt;- rep(G1[,2], G1[,3]) ## GENERATING 8736 Values


So the desired output are 8736 values of 400 when is ON and 0 when is OFF. Finally I need to simulate 100000 years of generation.
I don't know of any method, but here are some small improvements:

(1) You want mix to be TTF and TTR intertwined. An easy, vectorized way to do that is:

mix &lt;- c(rbind(TTF,TTR))


(2) You want sw to be an alternating vector of the capacity and 0. This can be vectorized as well:

cap &lt;- 400
sw &lt;- rep(c(cap,0), length(TTF))


(3) Next, simply find the index which is the first value above 8736 and rep that:

final &lt;- 8736
ind &lt;- which.max(cumsum(mix) &gt; final)
out &lt;- rep(sw[1:ind], round(mix)[1:ind])[1:final]


So full code would be:

MTTF &lt;- 2940
MTTR &lt;- 60
final &lt;- 8736
cap &lt;- 400
TTF &lt;--MTTF*log(runif(100))
TTR &lt;--MTTR*log(runif(100))

mix &lt;- c(rbind(TTF,TTR))
sw &lt;- rep(c(cap,0), length(TTF))
ind &lt;- which.max(cumsum(mix) &gt; final)
out &lt;- rep(sw[1:ind], round(mix)[1:ind])[1:final]