[R-SIG-Finance] Aggregating tick-by-tick data to seconds
G See
gsee000 at gmail.com
Sat Sep 15 18:58:17 CEST 2012
You can use `period.apply` (along with `endpoints()`) to apply any
function you like over non-overlapping time periods.
In this case, you want to sum columns 2 and 3 separately, so use
`period.apply()` with `FUN=colSums` on just the 2nd and 3rd columns.
period.apply(test[, 2:3], endpoints(test, 'secs'), FUN=colSums)
Then you can merge that with the results of `to.period`
> cbind(to.period(test[, 1], name="test"), period.apply(test[,
-1], endpoints(test, 'secs'), FUN=colSums))
test.Open test.High test.Low
test.Close up down
2012-09-12 16:30:00 144.39 144.39 144.38 144.38 5903 100
You could also use `period.sum()` on each column separately.
HTH,
Garrett
On Sat, Sep 15, 2012 at 10:57 AM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
> Hello,
>
> I have the following data (xts) (last two columns refer to up and down
> volumes).
>
>> head(test,20)
> [,1] [,2] [,3]
> 2012-09-12 16:30:00 144.39 300 0
> 2012-09-12 16:30:00 144.39 500 0
> 2012-09-12 16:30:00 144.39 300 0
> 2012-09-12 16:30:00 144.39 400 0
> 2012-09-12 16:30:00 144.39 300 0
> 2012-09-12 16:30:00 144.39 300 0
> 2012-09-12 16:30:00 144.39 100 0
> 2012-09-12 16:30:00 144.39 900 0
> 2012-09-12 16:30:00 144.39 200 0
> 2012-09-12 16:30:00 144.39 103 0
> 2012-09-12 16:30:00 144.39 197 0
> 2012-09-12 16:30:00 144.39 200 0
> 2012-09-12 16:30:00 144.39 500 0
> 2012-09-12 16:30:00 144.39 400 0
> 2012-09-12 16:30:00 144.39 100 0
> 2012-09-12 16:30:00 144.39 500 0
> 2012-09-12 16:30:00 144.39 300 0
> 2012-09-12 16:30:00 144.39 200 0
> 2012-09-12 16:30:00 144.39 103 0
> 2012-09-12 16:30:00 144.38 0 100
>
>
> I can use the to.period to aggregate the above data to seconds
>
> test.Open test.High test.Low test.Close
> 2012-09-12 16:30:00 144.39 144.47 144.37 144.38
> 2012-09-12 16:31:00 144.39 144.40 144.33 144.37
> 2012-09-12 16:32:00 144.37 144.45 144.37 144.45
> 2012-09-12 16:33:00 144.45 144.47 144.42 144.47
>
>
> but I am not sure how to do this for the volume columns. I would need to
> retrieve the volume sums per second.
>
> Is this functionality built into the xts package? Is there any other
> function that could be used to do something like that?
>
> Thanks & best regards,
> Costas
> _______________
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
More information about the R-SIG-Finance
mailing list