I have an irregularly spaced XTS time series (with POSIXct values as index type).

How can I build a new time series sampled at a let's say 10 minute interval, but with each sample moment aligned to a round time (13:00:00, 13:10:00, 13:20:00, ...). If a resampling moment doesn't fall exactly on a original series value, I want to take the previous one.

  • $\begingroup$ Could you provide an example? $\endgroup$ – Joshua Ulrich Dec 9 '10 at 16:23
  • 2
    $\begingroup$ If there ever was a list of FAQ to xts, this would score highly. Please look around here, search for [r] xts and peruse the r-sig-finance archives. $\endgroup$ – Dirk Eddelbuettel Dec 9 '10 at 16:24

For instance

tmp=zoo(rnorm(1000), as.POSIXct("2010-02-1")+(1:1000)*60)
tmp[endpoints(tmp, "minutes", 20)]

to subsample every 20 minutes. You might also want to check out to.minutes, to.daily, etc.

| cite | improve this answer | |
  • $\begingroup$ There is only a small issue - if I have ..., 14:59, 15:00, ..., it will retrieve 14:59 and not 15:00 as I'd like. Is there a way to make it return 15:00? I tried something like "seconds", 3601, but it doesn't work $\endgroup$ – Meh Dec 9 '10 at 18:39
  • $\begingroup$ Since the timestamps are irregular one can only make guarantees on the dt between data points. The actual points depend on the starting point of the series. If you fix your starting point to a round hour it will behave as you like... (assuming you have points every N minutes) $\endgroup$ – Dr G Dec 9 '10 at 18:46
  • $\begingroup$ This is exactly what I don't want - being dependent on the first point $\endgroup$ – Meh Dec 9 '10 at 19:04

I'm still not sure what you're trying to do and I still think an example would help, but I thought I'd guess that you may be interested in align.time.

# Compare this:
tmp[endpoints(tmp, "minutes", 20)]
# with this:
align.time( tmp[endpoints(tmp, "minutes", 20)], n=60*20 )
| cite | improve this answer | |
  • $\begingroup$ Consider any time series - outside temperature. I want to know what's the temperature at 11:00 wall clock, at 12:00 wall clock, .... I'll check your example later to see if it does this $\endgroup$ – Meh Dec 9 '10 at 19:05
  • $\begingroup$ As far as I can tell, this application of align.time after using endpoints is what Adal wanted (apart from regarding his mention of taking "the previous one" in the original question). Anyway, it is what I wanted, so thanks, Joshua. $\endgroup$ – Rahul Savani Feb 20 '11 at 8:54
  • $\begingroup$ Is there a version of align.time but to round down, not up? It also does exactly what I want but for that. (BTW, I have a crude way: if a1 is my XTS object and I want to round down to minute intervals, then index(a1)=index(a1)-60;align.time(a1,60)) $\endgroup$ – Darren Cook Nov 25 '11 at 9:32
  • 1
    $\begingroup$ @DarrenCook: There's isn't a version of align.time that does that. Regardless, it seems like a bad idea to align observations with a time prior to their occurrence. $\endgroup$ – Joshua Ulrich Nov 25 '11 at 14:07
  • 2
    $\begingroup$ @JoshuaUlrich One example is converting financial ticks in to a bar. 08:00 represents the minute from 08:00:00 to 08:00:59. 08:00 as an hour bar represents 08:00:00 to 08:59:59. (It is then consistent with how daily bars work, where 2011-11-25 represents trading on 2011-11-25 not on 2011-11-24.) I think this was the same behaviour the OP wanted. $\endgroup$ – Darren Cook Nov 27 '11 at 0:15

If a is an xts object with entries to second resolution, this knocks off all the seconds: index(a)=trunc(index(a),"mins")

You can also use this to round down to "hours" resolution too. But 10 minutes is not supported. For that you have to do this:


Or a=align.time.down(a,600) where you've defined:


('ve gone with that last choice in my own script.)

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.