Assuming that the first trade is the first observation per day, Garrett's
reply is pretty much what you want.  In one line - and a bit faster:

> hfdata <- xts(rnorm(13830),timeBasedSeq("2010-01-01 09:30/2010-01-10") +
sample(600,60))

> do.call(rbind,lapply(split(hfdata,'days'), first))


2010-01-01 09:35:31  0.7394613
2010-01-02 00:00:15 -1.6276554
2010-01-03 00:00:15 -1.7206578
2010-01-04 00:00:15 -1.6565504
2010-01-05 00:00:15 -0.5898241
2010-01-06 00:00:15  0.2934215
2010-01-07 00:00:15 -0.7946091
2010-01-08 00:00:15 -0.4867441
2010-01-09 00:00:15  0.4429763
2010-01-10 00:00:15  0.3289314
2010-01-11 00:00:15 -1.1578429

split.xts will split your data into a list of chunks by time, here 'days'.
 lapply iterates over this list and calls first, which will get the first
observation of each 'day'.  do.call(rbind works on a list to put everything
back into an xts object.

You could also do:

> apply.daily(hfdata, first)
                          [,1]
2010-01-01 23:59:44  0.7394613
2010-01-02 23:59:44 -1.6276554
2010-01-03 23:59:44 -1.7206578
2010-01-04 23:59:44 -1.6565504
2010-01-05 23:59:44 -0.5898241
2010-01-06 23:59:44  0.2934215
2010-01-07 23:59:44 -0.7946091
2010-01-08 23:59:44 -0.4867441
2010-01-09 23:59:44  0.4429763
2010-01-10 23:59:44  0.3289314
2010-01-11 00:01:32 -1.1578429

But you'll note the times now aggregate to the final time in each day.  I'll
think about adding an argument to period.apply that will let you set where
the times align to - much like how to.period works.  For now, I would stick
with the more verbose approach.

Best,
Jeff

On Tue, Jun 14, 2011 at 10:40 PM, Noah Silverman <noahsilverman@ucla.edu>wrote:

> Hi,
>
> I have a long series of hourly bars.  They are not aligned on the exact
> hour.  I used xts to convert actual tick data to hourly bars.
>
> I want to access the first trade of each day.  Is there an easy way to do
> this?
>
> Furthermore, what if I want to find the first trade that occurred after
> 6:30am each day?  (Or any other arbitrary time each day?)
>
>
> Here are a few rows of data:
>
> finalData[4880:4890,]
>                    ds.Open ds.High  ds.Low ds.Close          ret
> 2008-02-27 02:59:47 1461.75 1477.75 1457.25  1465.25  0.002220515
> 2008-02-27 03:59:32 1467.25 1474.25 1425.75  1443.25 -0.015128361
> 2008-02-27 04:59:30 1442.75 1477.25 1410.00  1431.25 -0.008349327
> 2008-02-27 05:59:59 1434.00 1438.75 1366.75  1366.75 -0.046112530
> 2008-02-27 09:59:59 1378.25 1439.25 1378.25  1428.00  0.043839205
> 2008-02-27 10:59:58 1427.75 1636.75 1395.50  1591.00  0.108087885
> 2008-02-27 11:59:59 1591.00 1592.25 1505.75  1551.25 -0.025301692
> 2008-02-27 12:59:58 1550.50 1562.25 1495.75  1525.00 -0.017066647
> 2008-02-27 13:14:59 1524.75 1584.25 1521.75  1545.50  0.013353073
> 2008-02-27 18:59:56 1545.50 1560.25 1518.25  1551.00  0.003552402
> 2008-02-27 19:59:53 1549.50 1549.50 1523.50  1524.00 -0.017561427
>
>
>
> Thanks!
>
>
>
> --
> Noah Silverman
> UCLA Department of Statistics
> 8117 Math Sciences Building
> Los Angeles, CA 90095
>
> _______________________________________________
> R-SIG-Finance@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.
>



-- 
Jeffrey Ryan
jeffrey.ryan@lemnica.com

www.lemnica.com
www.esotericR.com

	[[alternative HTML version deleted]]

