1

My loop is not working from the third else if.

I want that if that statement is true that Cumu_excess_se2 at that position is equal Excess_se at that position and from there to the end that Cumu_excess_se(t) =Cumu_excess(t-1) + Excess_se(t)

My vectors are:

  • Q is solar electricity available hourly
  • Excess_se is the excess to store per hour
  • Cumu_excess_se2 is the vector sum of the excess

The loop:

for (j in 1:length(Cumu_excess_se)) {
    Cumu_excess_se[j] = sum(Excess_se[1:j])

    se.to.h <- vector("numeric", length(Excess_se))
    for (t in 1:length(se.to.h)) {
        if (Q[t] > maxse) {
            se.to.h[t] = maxse
        } else if ((Q[t] < maxse) & (Cumu_excess_se2[t] == 0)) {
            se.to.h[t] = Q[t]
        } else if ((Q[t] < maxse) & (Cumu_excess_se2[t] >= maxse)) {
            (se.to.h[t] = maxse) & (Cumu_excess_se2[t] = Cumu_excess_se2[t - 1] - (maxse - Q[t]))
        } else if ((Q[t] < maxse) & (Cumu_excess_se2[t] < maxse) & (Cumu_excess_se2[t] > 0)) {
            (se.to.h[t] = Cumu_excess_se2[t]) & (Cumu_excess_se2[t + 1] = Excess_se[t + 1])
        }
    }
}
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • 1
    It'll be hard for users to answer your question unless you make it reproducable. Here are some tips on how to do this: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jruf003 Nov 15 '17 at 21:24
  • 1
    Can you clean up the formatting of your loops? I don't want to edit them in case I get the intention wrong, but I can't follow what they're all doing at the moment. Making your problem minimal may help (see the post @jruf003 links to) – Phil Nov 15 '17 at 21:40
  • 2
    @Phil Done: copy to clipboard and run `formatR::tidy_source()`. Really handy. – Rui Barradas Nov 15 '17 at 21:49
  • I think if you describe goal in words we can help better - for loops may not be necessary at all. I'm very confused by the line `(se.to.h[t] = maxse) & (Cumu_excess_se2[t] = Cumu_excess_se2[t - 1] - (maxse - Q[t]))` - what is the `&` supposed to be doing? – Gregor Thomas Nov 15 '17 at 21:53
  • I have a vector Q which is the energy that I can produce per hour, from there I need to deliver a maxse value per hour if possible to the vector [se.to.h] and the rest needs to be store in a vector [cumu_excess]. This cumu_excess is going to be used later when Q < maxse and then [cumu_excess] has to be updated. My vector Q looks like that repeated 365 Q(0.000, 0.000, 0.000, 0.000,0.000, 0.000, 0.000, 0.000, 5064.198, 30127.683, 44032.768, 56049.508, 59997.865, 58710.358, 50813.643, 36221.887, 9441.724, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000) – Victhalia Zapata Nov 16 '17 at 09:30

0 Answers0