Generate time sequence of a day with a minute diff

2019-08-09 20:00发布

问题:

I want to generate time sequence of a day by a minute difference using R like

00:00, 00:01, 00:02, ..., 23:59

For the same, I am using timeBasedSeq function of xts package with following lines of code

timerange1<- paste('T00:00','/','T23:59',' 12:00',sep="")
timeBasedSeq(timerange1)

But, I am not able to generate the sequence with this. Also, I do not understand what 12:00 mean in first line of code, i.e., how does it relate to minutes or hours or seconds.

Any help will be appreciated.

回答1:

You don't respect the required format: CCYYMMDD HHMMSS, in your case CCYYMMDD HHMM. Try:

library(xts)
timerange1 <- "20160106 0000/20160106 2359"
seqMinute <- format(timeBasedSeq(timerange1), "%H:%M")
length(seqMinute)
# [1] 1440
range(seqMinute)
# [1] "00:00" "23:59"


回答2:

Or from the comments,

format(seq(as.POSIXct("2013-01-01 00:00:00", tz="GMT"), 
                length.out=1440, by='1 min'), '%H:%M')