Hi R gurus! I need some help wrangling some data (screenshot attached to this post). Apologies, I don't even know where to begin so I don't have any R code yet...

I have a data frame of 1330 observations and I would like to sum the average column in chunks based on values in the m_off_ground column. Specifically, when a 0 occurs in the m_off_ground column, I want the sum calculation to 'restart' in the average column. Unfortunately, the 0s do not occur at regular intervals.

Furthermore, I'd like to create a column 'no_frames' which will tell me the number of columns/values included in each sum calculation.

So, the first one would be the sum of rows 2-11 in the average column (since there is a 0 in row 12 in the m_off_ground column) and the no_frames would equal 10.

I know there must be some for loop way to do this, or perhaps even a dplyr way but as a still relatively new R user, my brain can't wrap itself around how to do this....

Thanks so much for any help or advice!