Hi all

Possibly the wrong forum for this question, but I have a data frame (say x)
with the following fields: x$spread and x$time. These represent the spreads
when a price ticked in the market, and the time field is a milliseconds
since epoch field, and the spread field is a simple numeric (like 1e-4). I
would like to calculate a time-weighted average measure for each unique
spread value in my data frame, and am looking for an elegant way to do so.
The algorithn is something like:

for i =2:length(x) {
   spr = x$spread[i]
   time[spr] += x$time[i] - x$time[i-1]
}

avg = time / (sum(diff(x$time))    # where sum(diff(x$time)) is the total
time

The only trick is, I'm having difficulty expressing the algorithm in R -
usually I would do this via an associative array, however, I cant seem to
coax R to do what I want. Can anyone see what I am trying to do and possibly
have some suggestions?

Cheers
Rory

	[[alternative HTML version deleted]]

