0

I am a brand new R user. I have a very large amount of data over a range of time, with time in the first column increasing by 1/32 second increments. I want to extract the section of data that falls within a specific time range; for example, all of the data between 12:11:08PM and 12:11:11PM. However, the times in this column do not have any dates. Therefore, I could not figure out how to apply lubridate, POSIXct, or any other time functions since they all required dates. Is there a way for me to subset my data with a time only function? Thank you for your time.

time           id  result  
  12:11:08     10  200
  12:11:09     11  276 
  12:11:10     12  398 
  12:11:11     13  299 
  12:11:12     14  192  
  12:11:13     15  392
Meg
  • 1

1 Answers1

0

The problem is that POSIXct/POSIXlt stores time with an origin from "1970-01-01" with you timezone. So originally if you set the

as.POSIXct("12:11:10",format="%H:%M:%S")

it returns a full date format

[1] "2016-05-24 12:11:10 EEST"

You can easily subset time using default date settings (in this example function shows records after the "12:11:10"

 df<-data.frame(time=c("12:11:08","12:11:09","12:11:10","12:11:11","12:11:12","12:11:13"), 
               id=c("10", "11","12","13","14","15"),
               result=c("200","276","398","299","192","392"))

df[as.POSIXct(df$time,format="%H:%M:%S")>as.POSIXct("12:11:10",format="%H:%M:%S"),]

     time id result
4 12:11:11 13    299
5 12:11:12 14    192
6 12:11:13 15    392

Another great solution is to use chrone library

library(chrone)
df[chron(times=df$time)>chron(times="12:11:10"), ]