I want to get a vector (it will end up as the index of a zoo object) of POSIXct objects, spanning N separate periods. I.e. this code works for the case of N=2:
from=as.POSIXct( c('2012-01-04 09:00:00','2012-01-04 11:00:00'), tz="UTC")
to=as.POSIXct( c('2012-01-04 09:15:00','2012-01-04 11:10:00'), tz="UTC" )
index=c( seq(from[1],to[1],by=60), seq(from[2],to[2],by=60) )
class(index) #"POSIXct"
How do I make that code generic, for any number of time periods? I tried mapply() and it got weird. I seemed to end up with a list of numeric types, not POSIXct objects, and then in my various tries to convert that to POSIXct I sometimes got error messages about needing an origin. To confuse matters further, if the two periods are the same length (example below), then mapply returns a matrix, not a list. Which, if anything, seems harder to get into a single vector of POSIXct objects.
Maybe this is a question about converting the return type(s) of mapply; but I also started to feel maybe I’m missing an easier way?
Here is the test data for two periods of the same length:
from=as.POSIXct( c('2012-01-04 09:00:00','2012-01-04 11:00:00'), tz="UTC")
to=as.POSIXct( c('2012-01-04 09:15:00','2012-01-04 11:15:00'), tz="UTC" )
try this: