Resolved: How to compute a weighted loop?

In this post, we will see how to resolve How to compute a weighted loop?


This is a portion of my data frame:
I need to compute a loop that enables me to compute this logic:
  • if 652 > 500, then bin = 92.02, and the remaining (652 - 500) goes to the next bin;
  • because the bin2 contains the 152, remaining from the previous row, only 348 msec from 618 are needed. In that case, a weighted average is needed: ((152*92.02) + (348*97.09)) / 500 which gives 95.55.
  • (I don’t expect to have IBI values under 500 in the data).

enter image description here
The final result should be something like this:

Best Answer:

This does what you want: that said, there will be a problem if (when) remainder goes over 500, so you should complete your rules to deal with that case.

If you have better answer, please add a comment about this, thank you!