[R-SIG-Finance] using [ on xts object

Joshua Ulrich josh.m.ulrich at gmail.com
Fri Dec 23 23:38:02 CET 2011


On Fri, Dec 23, 2011 at 3:17 PM, financial engineer
<fin_engr at hotmail.com> wrote:
> Dear Joshua,
>
> thanks for your response.
>
> I have done the search and have rewritten the code as follows:
>
> dt<-cbind(Z11[,1],Z11[,2]) which returned the following:
>
> [49994,] "2011-10-31" "0833"
>  [49995,] "2011-10-31" "0833"
>  [49996,] "2011-10-31" "0833"
>  [49997,] "2011-10-31" "0833"
>  [49998,] "2011-10-31" "0833"
>  [49999,] "2011-10-31" "0833"
>
> and then I tried the following:
> as.POSIXct(as.Date(strptime(as.character(index(dt)),'%Y%m%d %H:%M')))
>
> however, the above returns only NA. It seems like I am making a silly error
> somewhere, so any help in fixing it would be appreciated.
>
Errors, plural.

1) You cbind the "date" and "time" columns into a _matrix_ dt.  Then
you try to extract the index(dt) of the matrix.  Matrix class objects
don't have an index attribute.
2) You pass a matrix to strptime.  ?strptime says "strptime converts
character _vectors_ to class POSIXlt" (emphasis added).
3) Even if you didn't make the first two errors, you use as.Date() to
effectively remove the time component you set out to add.

My intent isn't to be rude.  These 'issues' are the result of not
understanding some very basic concepts.  Doing the work for you and
giving you the answer won't help you in the long-term.  So, take a
step back and examine the results of the individual commands to find
where the output isn't what you expect.  Then read the documentation
to find out why.

> thanks,
> Bobby
>

Regards,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com



>> From: josh.m.ulrich at gmail.com
>> Date: Fri, 23 Dec 2011 14:00:18 -0600
>> Subject: Re: [R-SIG-Finance] using [ on xts object
>> To: fin_engr at hotmail.com
>> CC: r-sig-finance at r-project.org
>
>>
>> Dear Mr. or Mrs. Engineer,
>>
>> On Fri, Dec 23, 2011 at 1:50 PM, financial engineer
>> <fin_engr at hotmail.com> wrote:
>> >
>> > I have the following sample tick data set (Z11):
>> > 24990 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24991 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24992 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24993 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24994 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24995 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24996 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24997 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24998 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>> > 24999 á2011-10-31 á á á 0421 á á á á8900 á á á á áB
>
>> >
>> > where the second field is the date, third- time, fourth - price, and
>> > fifth - bid, ask, or trade
>> >
>> > I have converted the above into a xts object using the following command
>> >
>> > z<-xts(Z11[,-1],order.by=as.Date(Z11[,1]))
>> >
>> > which has resulted in data in the following form
>> > 2011-10-31 "0421" á á "8900" á á á"B"
>> > 2011-10-31 "0421" á á "8900" á á á"B"
>> > 2011-10-31 "0421" á á "8900" á á á"B"
>> > 2011-10-31 "0421" á á "8900" á á á"B"
>> > 2011-10-31 "0421" á á "8900" á á á"B"
>> > 2011-10-31 "0421" á á "8900" á á á"B"
>> > 2011-10-31 "0421" á á "8900" á á á"B"
>> > 2011-10-31 "0421" á á "8900" á á á"B"
>> >
>> > I want to split my data based on the hour of the day, áso I was trying
>> > to use the following
>
>> > z['2011-10-31 04/'] to get the data starting from the 04 hour through
>> > the end of the day,
>> >
>> > but it returns the following:
>> > z['2011-10-31 04/']
>> > á á trade_time trade_price trade_type
>
>> >
>> > can anyone pls. suggest how I can split the data into hourly intervals
>> > from this xts object.....thanks!
>> >
>> You can't. You created an xts object with a Date class index. The
>> Date class doesn't contain any information about time of day. Use a
>> date/time class (read ?DateTimeClasses).
>>
>> Subsetting xts objects by time of day has been discussed several times
>> on this list. Please search the list archives (e.g. via rseek.org).
>>
>> Regards,
>> --
>> Joshua Ulrich | FOSS Trading: www.fosstrading.com



More information about the R-SIG-Finance mailing list