Generate time sequence of a day with a minute difference in R

天大地大妈咪最大 提交于 2019-12-24 17:45:59

问题


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')


来源:https://stackoverflow.com/questions/34647685/generate-time-sequence-of-a-day-with-a-minute-difference-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!