[R-SIG-Finance] Selecting a time range from an XTS series?

Jeff Ryan jeff.a.ryan at gmail.com
Thu Sep 23 17:22:16 CEST 2010


Ulrich,

Misread your question.  ISO subsetting ins't going to work on  this,
but here is a solution that will.

# 8:30 - 15:00
x <- timeBasedSeq('2010-01-01 08:30/2010-01-01 15:00')
x <- xts(1:length(x), x)
x[.indexmin(x) %in% 10:20]
                    [,1]
2010-01-01 09:10:00   41
2010-01-01 09:11:00   42
2010-01-01 09:12:00   43
2010-01-01 09:13:00   44
2010-01-01 09:14:00   45
2010-01-01 09:15:00   46
2010-01-01 09:16:00   47
2010-01-01 09:17:00   48
2010-01-01 09:18:00   49
2010-01-01 09:19:00   50
2010-01-01 09:20:00   51
2010-01-01 10:10:00  101
2010-01-01 10:11:00  102
2010-01-01 10:12:00  103
2010-01-01 10:13:00  104
2010-01-01 10:14:00  105
2010-01-01 10:15:00  106
2010-01-01 10:16:00  107
2010-01-01 10:17:00  108
2010-01-01 10:18:00  109
2010-01-01 10:19:00  110
2010-01-01 10:20:00  111
2010-01-01 11:10:00  161
2010-01-01 11:11:00  162
2010-01-01 11:12:00  163
2010-01-01 11:13:00  164
2010-01-01 11:14:00  165
2010-01-01 11:15:00  166
2010-01-01 11:16:00  167
2010-01-01 11:17:00  168
2010-01-01 11:18:00  169
2010-01-01 11:19:00  170
2010-01-01 11:20:00  171
2010-01-01 12:10:00  221
2010-01-01 12:11:00  222
2010-01-01 12:12:00  223
2010-01-01 12:13:00  224
2010-01-01 12:14:00  225
2010-01-01 12:15:00  226
2010-01-01 12:16:00  227
2010-01-01 12:17:00  228
2010-01-01 12:18:00  229
2010-01-01 12:19:00  230
2010-01-01 12:20:00  231
2010-01-01 13:10:00  281
2010-01-01 13:11:00  282
2010-01-01 13:12:00  283
2010-01-01 13:13:00  284
2010-01-01 13:14:00  285
2010-01-01 13:15:00  286
2010-01-01 13:16:00  287
2010-01-01 13:17:00  288
2010-01-01 13:18:00  289
2010-01-01 13:19:00  290
2010-01-01 13:20:00  291
2010-01-01 14:10:00  341
2010-01-01 14:11:00  342
2010-01-01 14:12:00  343
2010-01-01 14:13:00  344
2010-01-01 14:14:00  345
2010-01-01 14:15:00  346
2010-01-01 14:16:00  347
2010-01-01 14:17:00  348
2010-01-01 14:18:00  349
2010-01-01 14:19:00  350
2010-01-01 14:20:00  351

HTH,
Jeff

On Wed, Sep 22, 2010 at 10:25 AM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
> Ulrich,
>
> # create a time sequence from the beginning of 2010 to the 3rd, by hours
> # Note that the level of granularity is determined by the maximum precision
> # specified of both sides.  Here it is to the hour level - 65 hours in
> the example:
>
> x <- timeBasedSeq('2010/2010-01-03 15')
>
> head(x)
> [1] "2010-01-01 00:00:00 CST" "2010-01-01 01:00:00 CST"
> [3] "2010-01-01 02:00:00 CST" "2010-01-01 03:00:00 CST"
> [5] "2010-01-01 04:00:00 CST" "2010-01-01 05:00:00 CST"
>
>
> xts(1:length(x),x)['T08:00:00/T10:00:00']
>                    [,1]
> 2010-01-01 08:00:00    9
> 2010-01-01 09:00:00   10
> 2010-01-01 10:00:00   11
> 2010-01-02 08:00:00   33
> 2010-01-02 09:00:00   34
> 2010-01-02 10:00:00   35
> 2010-01-03 08:00:00   57
> 2010-01-03 09:00:00   58
> 2010-01-03 10:00:00   59
>
> I'll add some examples into the documentation of ?.parseISO8601.  In
> general, the actual ISO8601 standard is the place to look first, as
> xts implements most of the subsetting functionality defined there.
>
> Best,
> Jeff
>
>
> On Wed, Sep 22, 2010 at 10:10 AM, Ulrich Staudinger
> <ustaudinger at gmail.com> wrote:
>> Hi there,
>>
>> I am looking for a way to fetch a specific time range (not a date range)
>> from an XTS object (a price series).
>>
>> As input, I have an xts object that looks roughly like this:
>>
>> ..........
>> 2010-08-20 19:54:49   2537.5
>> 2010-08-20 19:55:58   2550.0
>> 2010-08-20 19:58:59   2587.5
>> 2010-08-20 19:59:42   2675.0
>> 2010-08-20 20:02:50   2675.0
>> 2010-08-20 20:03:50   2675.0
>> 2010-08-20 20:06:59   2675.0
>> 2010-08-20 20:07:59   2675.0
>> 2010-08-20 20:08:59   2687.5
>> 2010-08-20 20:09:57   2750.0
>> 2010-08-20 20:10:35   2750.0
>> 2010-08-20 20:11:59   2750.0
>> 2010-08-20 20:12:58   2750.0
>> 2010-08-20 20:13:49   2812.5
>> ........
>>
>>
>> I would like to know if there is a way to fetch something like ****-**-**
>> **:10:00 to ****-**-** **:20:00. Meaning, over the whole data set, all
>> information between 10th and 20th minute.
>> So, I would expect something like this:
>>
>> ....
>> 2010-08-19 23:10:01     123
>> 2010-08-19 23:19:01     123
>> 2010-08-19 23:20:01     123
>> 2010-08-20 00:10:01     123
>> 2010-08-19 00:11:01     123
>> 2010-08-19 00:20:01     123
>> 2010-08-19 01:10:01     123
>> ...
>>
>> Is such a function available?
>>
>> Thanks,
>> Ulrich
>>
>> --
>> Ulrich Staudinger
>> ustaudinger at activequant.org
>> http://www.activequant.org
>>
>>        [[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.
>> -- Also note that this is not the r-help list where general R questions should go.
>>
>
>
>
> --
> Jeffrey Ryan
> jeff.a.ryan at gmail.com
>



-- 
Jeffrey Ryan
jeff.a.ryan at gmail.com



More information about the R-SIG-Finance mailing list