Data setting
Set up input frames first. We create two versions of data frames: A and B just use the character columns for time, and At and Bt use the chron package "times" class for time (which takes precedence over the "character" class, which you can add and subtract):
LinesA <- "OBS ID StartTime Duration Outcome 1 01 10:12:06 00:00:10 Normal 2 02 10:12:30 00:00:30 Weird 3 01 10:15:12 00:01:15 Normal 4 02 10:45:00 00:00:02 Normal" LinesB <- "OBS ID Time 1 01 10:12:10 2 01 10:12:17 3 02 10:12:45 4 01 10:13:00" A <- At <- read.table(textConnection(LinesA), header = TRUE, colClasses = c("numeric", rep("character", 4))) B <- Bt <- read.table(textConnection(LinesB), header = TRUE, colClasses = c("numeric", rep("character", 2))) # in At and Bt convert times columns to "times" class library(chron) At$StartTime <- times(At$StartTime) At$Duration <- times(At$Duration) Bt$Time <- times(Bt$Time)
sqldf with class times.
Now we can perform the calculation using the sqldf package. We use method="raw" (which does not assign classes to output), so we must assign the class "times" to output "Time" :
library(sqldf) out <- sqldf("select Bt.OBS, ID, Time, Outcome from At join Bt using(ID) where Time between StartTime and StartTime + Duration", method = "raw") out$Time <- times(as.numeric(out$Time))
Result:
> out OBS ID Time Outcome 1 1 01 10:12:10 Normal 2 3 02 10:12:45 Weird
With the sqldf development version, this can be done without using method="raw" , and the "Time" column will be automatically set to the "times" class using the sqldf class assignment heuristic:
library(sqldf) source("http://sqldf.googlecode.com/svn/trunk/R/sqldf.R")
sqldf with character class
In fact, it is not possible to use the "times" class, performing all time calculations in sqlite from string characters using the sqlite strftime function. Unfortunately, the SQL statement is a little involved:
sqldf("select B.OBS, ID, Time, Outcome from A join B using(ID) where strftime('%s', Time) - strftime('%s', StartTime) between 0 and strftime('%s', Duration) - strftime('%s', '00:00:00')")
EDIT:
A series of fixes that fixed the grammar added additional approaches and fixed / improved read.table instructions.
EDIT:
Simplified / improved final sqldf statement.