[R-SIG-Finance] tseries and xts - time indexing in an easier way?

Jeff Ryan jeff.a.ryan at gmail.com
Thu Dec 17 19:54:34 CET 2009


Ivan,

xts requires ISO 8601 because it is the only agreed upon format that
is unambiguous in its definition.

You might be better of using zoo with yearmon.  While you'll lose the
ISO style subsetting, it seems like that isn't an issue now anyway.
The other advantage to xts is speed (as much is done with C code
internally).  For monthly data in finance this wouldn't seem to be of
much value though.  Since your series are likely not very long.

You could also use strptime or similar to format your 'request' string
into something along the lines if ISO.

HTH
Jeff

On Thu, Dec 17, 2009 at 12:46 PM, Zhang, Ivan
<ivan.zhang at bankofamerica.com> wrote:
> Hi all,
>
>
>
> I've been experimenting on tseries package, namely ts class and I've
> been having to do a workaround to have time indexing. I was wondering if
> there's an easier way.  I am working with monthly data. i.e. I would
> like to be able to use yearmon class easily.
>
>
>
> Say I have a bunch of time series with different intervals but they have
> overlapping time frame.
>
>
>
> If I wanted to loop through them relative to some timepoint, I would
> either have to translate the time into an index, and do the
> corresponding arithmetic to calculate what that would correspond to in
> another time series.
>
>
>
> The alternate way would be to find the index by matching the time, but
> that doesn't work so well as match doesn't always come up due to
> numerical precision issues.
>
>
>
> ts1 = ts(1:10, start = c(2000,1), frequency = 12)
>
> specialtime = time(ts1)[ts1==5]
>
>
>
> ts2 = ts(1:20, start = c(2000,8), frequency = 12)
>
>
>
>
>
>    for( i in 1:match(specialtime, time(ts2)))
>
>                                ts2[i] = ...
>
>
>
>
>
> so what I end up doing is within the loop, or wherever I need to set a
> value, I end up doing:
>
>
>
> match(yearmon(specialtime), yearmon(time(ts2)))
>
>
>
> Somehow, that seems unusually unwieldly as compared to being directly
> able to index via time.
>
>
>
> So I've also tried xts with time index, however, the time index needs to
> be in ISO format whereas the times that I have is like variable
> specialtime where it's of the form 2005.3
>
>
>
> I tried converting all of the variables into xts, I also tried changing
> the indexing class into yearmon
>
> Ts3 = convertIndex(as.xts(ts2), "yearmon")
>
>
>
> Now I can do aa = time(Ts3)[1] and it is an yearmon class,
>
>
>
> however, I still can't access anything using Ts3[aa]. It gives me:
>
>
>
> Error in as.POSIXlt.character(x, tz, ...) :
>
>  character string is not in a standard unambiguous format
>
>
>
> So I'm not sure if I'm missing something obvious, if anyone could help?
>
>
>
> Thanks,
>
>
>
> Ivan Zhang
>
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



-- 
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



More information about the R-SIG-Finance mailing list