I have a set of pre-specified breaks, 

```
breaks = 2:7

```

that form a set of bins: `(2,3] (3,4] (4,5] (5,6] (6,7]`. Then I have a dataset looking like this 

```
set.seed(42)
data = cbind.data.frame(time = cumsum(abs(rnorm(10))), value = rnorm(10))
> data
       time      value
1  1.370958  1.3048697
2  1.935657  2.2866454
3  2.298785 -1.3888607
4  2.931648 -0.2787888
5  3.335916 -0.1333213
6  3.442040  0.6359504
7  4.953562 -0.2842529
8  5.048222 -2.6564554
9  7.066645 -2.4404669
10 7.129359  1.3201133

```

Where `time` is to be seen as the time when `value` is updated, thus value is piecewise constant. 
What is a clever way to compute the weighted average of `value` for each of the bins above? My desired result would look like this: 

```
     bin      mean
1  (2,3] -0.546621
2  (3,4]       ...

```

where I calculated the time-weighted mean as 

```
(data$time[3]-2) * data$value[3] + 
  (data$time[4]-data$time[3])*data$value[4] + 
  (3-data$time[5]) * data$value[5]

```

Note that the problem is to calculate the weighted mean from the borders of the bins. Otherwise I could just do a `weighted.mean` and choose the `weights` as `diff(data$time)`. The only possible strategy I came up with is to add rows to `data` where time is the break times and the previous value is copied, i.e:

```
> data.mod
       time      value
1  1.370958  1.3048697
2  1.935657  2.2866454
3  2.000001  2.2866454
4  2.298785 -1.3888607
5  2.931648 -0.2787888
6  3.000001 -0.2787888
7   ...

```

and then I `cut`, `split` and take `weighted.mean` and all works out. But the only way to add those rows is a slow loop and for my real data `length(breaks)` is anywhere between 500 and 20,000, `dim(data)[1]` is roughly 10,000 - 50,000 and I have to repeat this operation at least 2,000 times so speed is appreciated.