From the comments to the question, the problem is not how to calculate the difference bewteen two times using sqlite. The times have already been differenced before sending them to sqlite and the problem is that the resulting "difftime" class column is converted to numeric when it is sent to sqlite and when it is retrieved back to R, sqldf does not know how to convert that number back to a "difftime" class object because it does not know which units to use.
Here is a self contained example to illustrate:
library(sqldf)
now <- Sys.time()
now2 <- now + 1
dif <- difftime(now2, now)
DF <- data.frame(dif)
sqldf("select * from DF")
## Error in asfn(rs[[i]]) : need explicit units for numeric conversion
There are several approaches to this:
1) do not use a "difftime" object in the first place. Use the number of seconds or minutes or whatever as a numeric variable:
DF1 <- data.frame(dif = as.numeric(dif))
sqldf("select * from DF1")
## dif
## 1 1
2) perform the differencing in SQL rather than in R so that a "difftime" column is not created in the first place:
DF2 <- data.frame(now, now2)
sqldf("select now2 - now as dif from DF2")
## dif
## 1 1
3) use sqldf(..., method = "raw") to prevent it from trying to convert back to "difftime" class:
sqldf("select * from DF")
## dif
## 1 1
4) make sure that the original "difftime" column is renamed in the output so that it cannot associate it with the original "difftime" column and so the heuristic that assigns classes will not try to convert it.
sqldf("select dif as dif2 from DF")
## dif2
## 1 1
5) Use the name__class method of sqldf (note the double underscore to specify the class to convert to:
sqldf("select dif as dif__numeric from DF", method = "name__class")
## dif
## 1 1
sqldf?sqldfas well.