Skip to content

Query about creating time sequences

10 messages · Shivam, michael.weylandt at gmail.com (R. Michael Weylandt, Jeff Newmiller +2 more

One (somewhat kludgy) way would be to use seq() to make one day's worth of times then to pass those to outer() to add in the needed days and then coerce the whole thing back to a sorted vector. 

I'm not at a computer right now so this won't be quite right but something like


x <- seq(x.start.first.day, x.end.first.day, by = "sec")

y <- 24*60*60 *(1:n.days)

sort(as.vector(outer(x, y, "+")))

Changing the order of x and y might make the sort unnecessary. 

M
On May 25, 2012, at 1:14 PM, Shivam <shivamsingh at gmail.com> wrote:

            
1 day later
#
On Fri, May 25, 2012 at 1:14 PM, Shivam <shivamsingh at gmail.com> wrote:
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"]
#
Try this:

# Setting TZ is optional, but I find it helps me to be more aware of 
# timezone effects
Sys.setenv( TZ="Asia/Kolkata")
library(lubridate)

pdates <- as.POSIXct( fdates )
hstart <- new_period( hour=9, minute=15 )
hend <- new_period( hour=15, minute=30 )
mperiod <- new_period( minute=15 )
numperday <- (hend-hstart)/mperiod
dtms <- expand.grid( dt=pdates, tm=hstart + mperiod * seq( from=0, 
to=numperday ) )
dtms$dtm <- with( dtyms, dt + tm )
dtms <- dtms[ order( dtms$dtm ), ]

You can discard everything but dtms$dtm once it has been created.
On Mon, 28 May 2012, Shivam wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
#
On Sun, May 27, 2012 at 7:03 PM, Shivam <shivamsingh at gmail.com> wrote:
Why does the number of days "make it difficult to achieve"?  The
number of days does not affect the code at all.  Is there some aspect
of the problem you haven't mentioned?
#
On Sun, May 27, 2012 at 8:01 PM, Shivam <shivamsingh at gmail.com> wrote:
If dd is a vector of the dates you want then just change the last line
to choose only those using as.Date(tseq, tz = "") %in% dd as below:

dd <- as.Date(c("2011-01-03", "2011-01-04")) ##

from <- as.POSIXct(paste(dd[1], "09:15:00")) ##
to <- as.POSIXct(paste(tail(dd, 1), "15:30:00")) ##

tseq <- seq(from, to, "1 min")

tt <- format(tseq, "%H:%M:%S")
tresult <- tseq[tt >= "09:30:00" & tt <= "15:30:00" & as.Date(tseq, tz
= "") %in% dd] ##
#
Depending on your exchange of interest, you might also find some of
the functions of the timeDate package helpful, e.g., holidayNYSE() --
it will miss the day the market was closed for extraordinary
circumstances, but it seems to do a very good job. [Disclaimer: I
haven't used it myself extensively]

Michael

On Sun, May 27, 2012 at 8:26 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote: