Question

I have the following data frame:

>head(d)
Smed                Time TTFB
1  186 2014-03-19 20:13:00    6
2  196 2014-03-19 21:23:00    0
3  186 2014-03-19 19:33:00   22
4  186 2014-03-19 14:43:00  134
5  186 2014-03-19 23:53:00   36
> str(d)
 $ Smed          : Factor w/ 71 levels "1033","1038",..: 14 16 14 14 14 14 47 14 14 68 ...
 $ Time          : POSIXct, format: "2014-03-19 20:13:00" "2014-03-19 20:13:00" "2014-03-19 20:13:00" "2014-03-19 20:13:00" ...
 $ TTFB          : int  6 0 22 134 36 96 61 37 28 19 ...

How do i do a time comparison with POSIXct time format to get data say between 18:13 and 23:20

Was it helpful?

Solution

POSIXct understands < and >, so you simply do this:

set.seed(1)
d <- data.frame(Time=as.POSIXct("2014-03-19")+24*60*60*runif(100))
index <- as.POSIXct("2014-03-19 18:00:00")<=d$Time &
         d$Time<=as.POSIXct("2014-03-19 23:20:00")
d[index,]

OTHER TIPS

Try something like

start <- as.POSIXct("2014-03-19 18:13:00")
end <- as.POSIXct("2014-03-19 23:10:00")
d[start < d$Time & d$Time < end, ]

Hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top