With a dataset that looks like this:
Index x y
2012-07-24 07:00:00 0.1 0
2012-07-24 07:15:00 0.2 1
2012-07-24 07:30:00 0.3 0
2012-07-24 07:45:00 0.2 0
2012-07-24 08:00:00 0.3 1
2012-07-24 08:15:00 0.4 1
2012-07-24 08:30:00 0.4 1
2012-07-24 08:45:00 0.5 0
2012-07-24 09:00:00 0.6 0
I currently have a for loop that goes through this large dataset and essentially calculates y/x each time y != 0. An additional calculation I am trying to make is the average x within an event, where an event is defined as any consecutive string in y of non-zero values. Using the dataset I have provided above, there are two events: one of length 1, and one of length 3.
For the second event of length 3, in my for loop, I would like to return three values, one for each step through the loop: (0.3), (0.3+0.4)/2, and (0.3+0.4+0.4)/3.
I am having trouble determining what the most efficient way to do this. I have looked at some previous posts that mainly are looking for finding indices of zero-values using rle and patterns like which(x !== 0).
Any help is greatly appreciated.