[R] Query about creating time sequences

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 27 23:12:24 CEST 2012


On Fri, May 25, 2012 at 1:14 PM, Shivam <shivamsingh at gmail.com> wrote:
> Hi All,
>
> I have a query about time based sequences. I know such questions have been
> asked a lot on forums, but I couldnt find the exact thing that I was
> looking for.
>
> I want to create a time-based sequence which will mimic the trading window
> AND would span multiple days. Something like below:
>
> "2011-01-03 09:15:00 IST"
> "2011-01-03 09:15:01 IST"
> ....
> ....
> ....
> "2011-01-03 15:29:59 IST"
> "2011-01-03 15:30:00 IST"
> "2011-01-04 09:15:00 IST"
> "2011-01-04 09:15:01 IST"
> ....
> ....
> ....
> "2011-01-04 15:29:59 IST"
> "2011-01-04 15:30:00 IST"
>
> Kindly notice the change of date in the sequence.
>
> The Indian Equity markets open at 09:15:00 and close at 15:30:00. I have
> equity data that spans 124 days, and I need to create a corresponding
> sequence which I will later use to regularize the irregular dataset to make
> a regular time-series.
>
> I was able to accomplish this task for a single day (i.e. creating a
> sequence then merging my dataset with it and use na.locf to make my dataset
> regular) but am unable to create a sequence for 'n' number of days. Can
> anyone help me with this?
>
> If it is of any help, I have a file which contains all the dates for which
> I need the sequence. The dput of the file is placed at the end of the
> email.
>
> One option is to create sequences for the entire days and then later remove
> all these records after merging. Although I havent checked the feasibility
> of this method, it would be complex and more so it will increase the data
> four folds (I already have 2 million records in the dataframe which I have
> to make regular).
>
> Another approach that I could think of was to make a timebased sequence
> based on the date from the file and then use a loop to append one sequence
> after another. But am not having much success there either.
>
> Any kind of help would be greatly appreciated.
>
> Thanks and regards,
> Shivam
>

Create a minute by minute sequence of datetimes (tseq) from the first
datetime to the last datetime and then extract those datetimes whose
times (tt) lie between the desired times of day:

from <- as.POSIXct("2011-01-03 09:15:00:00")
to <- as.POSIXct("2011-01-04 15:30:00")
tseq <- seq(from, to, "1 min")

tt <- format(tseq, "%H:%M")
tseq[tt >= "09:30" & tt <= "15:30"]


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list