Create some reproducible data first so that we can help you better:
require(xts) set.seed(1) X = data.frame(When = as.Date(seq(from = ISOdatetime(2012, 01, 01, 00, 00, 00), length.out = 100, by="1 hour")), Measurements = sample(1:20, 100, replace=TRUE))
Now we have a data frame with 100 hourly observations, where the dates start from 2012-01-01 00:00:00 and end at 2012-01-05 03:00:00 (the time is in a 24-hour format).
Secondly, convert it to an XTS object.
X2 = xts(X$Measurements, order.by=X$When)
Third, learn how to subset a specific time window.
X2['T04:00/T08:00'] # [,1] # 2012-01-01 04:00:00 5 # 2012-01-01 05:00:00 18 # 2012-01-01 06:00:00 19 # 2012-01-01 07:00:00 14 # 2012-01-01 08:00:00 13 # 2012-01-02 04:00:00 18 # 2012-01-02 05:00:00 7 # 2012-01-02 06:00:00 10 # 2012-01-02 07:00:00 12 # 2012-01-02 08:00:00 10 # 2012-01-03 04:00:00 9 # 2012-01-03 05:00:00 5 # 2012-01-03 06:00:00 2 # 2012-01-03 07:00:00 2 # 2012-01-03 08:00:00 7 # 2012-01-04 04:00:00 18 # 2012-01-04 05:00:00 8 # 2012-01-04 06:00:00 16 # 2012-01-04 07:00:00 20 # 2012-01-04 08:00:00 9
Fourth, use this information with apply.daily and any function you want:
apply.daily(X2['T04:00/T08:00'], mean) # [,1] # 2012-01-01 08:00:00 13.8 # 2012-01-02 08:00:00 11.4 # 2012-01-03 08:00:00 5.0 # 2012-01-04 08:00:00 14.2
Update: custom endpoints
After reading your question again, I see that I misinterpreted what you wanted.
It seems that you want to accept the value of a 24-hour period, not necessarily from midnight to midnight.
To do this, you need to take apply.daily and use period.apply with custom endpoint s instead, for example:
# You want to start at 7AM. Find out which record is the first one at 7AM. A = which(as.character(index(X2)) == "2012-01-01 07:00:00")