I am trying to calculate the mean of values in a column (y) that are being sequentially counted (1, 2, 3, etc.) within another column (x). An example dataframe is shown below.
> df
x y
1 1 15
2 2 20
3 4 16
4 5 12
5 6 17
6 8 14
7 9 13
8 10 19
I would like to get a vector result that will read out the mean values of numbers in a numerical sequence. The desired vector would read: 17.5 15 15.33333
I am not sure the best way to produce this desired result, but I have tried and failed to make a for loop using diff(df[,1]) to find the breakpoints.
Any help anyone could provide would be appreciated. This is a small example dataset, but the goal is to apply it to a large dataset.