How to re-sample an XTS time series in R?

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.

-
Could you provide an example? –  Joshua Ulrich Dec 9 '10 at 16:23
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. –  Dirk Eddelbuettel Dec 9 '10 at 16:24

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 )

-
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 –  Adal Dec 9 '10 at 19:05
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. –  Rahul Feb 20 '11 at 8:54
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)) –  Darren Cook Nov 25 '11 at 9:32
@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. –  Joshua Ulrich Nov 25 '11 at 14:07
@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. –  Darren Cook Nov 27 '11 at 0:15
library(xts)
?endpoints


Fo 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.

-
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 –  Adal Dec 9 '10 at 18:39
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) –  Dr G Dec 9 '10 at 18:46
This is exactly what I don't want - being dependent on the first point –  Adal Dec 9 '10 at 19:04

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:

x=as.POSIXlt(index(a))
x$sec[]=0;x$min[]=x\$min[]%/%10
index(a)=x


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

align.time.down=function(x,n){index(x)=index(x)-n;align.time(x,n)}


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

-