从R中的时间戳获取时间

时间:2014-12-09 15:14:56

标签: r time time-series

我的数据框如下:

df<- structure(list(timeStamp = structure(c(1388308300, 1388308310, 
                                       1388308320, 1388308330, 1388308340, 1388308350, 1388308360, 1388308370, 
                                       1388308380, 1388308390), class = c("POSIXct", "POSIXt"), tzone = ""), 
               Bid = c(NA, 15.2, 15.2, 15.2, 15.2, 15.2, 15.2, 15.2, 
                          15.2, 15.2), 
               Ask = c(NA, 15.05, 15.05, 15.05, 15.05, 15.05, 15.05, 15.05, 15.05, 15.05)), 
              .Names = c("timeStamp","Bid", "Ask"), row.names = c(NA, 10L), class = "data.frame")

我可以轻松地提取日期:

df$dates <- as.Date(df$timeStamp)

然而,我不知道如何度过时光。我需要这个按时间过滤数据集,与日期无关(例如time> "06.00.00")。没有as.Time()函数,

我试过了:

strptime(df$timeStamp,format="%H:%M:%S")

as.POSIXct(df$timeStamp, format="%H:%M:%S")

但它们不起作用......

1 个答案:

答案 0 :(得分:3)

library(chron)
df$times <- times(format(df$timeStamp, '%H:%M:%S'))
df[df$times > times("10:12:00"),]
#             timeStamp  Bid   Ask    times
#4  2013-12-29 10:12:10 15.2 15.05 10:12:10
#5  2013-12-29 10:12:20 15.2 15.05 10:12:20
#6  2013-12-29 10:12:30 15.2 15.05 10:12:30
#7  2013-12-29 10:12:40 15.2 15.05 10:12:40
#8  2013-12-29 10:12:50 15.2 15.05 10:12:50
#9  2013-12-29 10:13:00 15.2 15.05 10:13:00
#10 2013-12-29 10:13:10 15.2 15.05 10:13:10