[R] timediff

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Jun 20 12:56:07 CEST 2006


"COMTE Guillaume" <g.comte at alliance-ir.net> writes:

> I've got 2 dates like these :
> "2006-02-08 17:12:55"
> "2006-02-08 17:15:26"
> I wish to get the middle of these two date :
> "2006-02-08 17 :14 :10"
> Is there is a function in R to do that ?

Well, 

> x1 <- as.POSIXct("2006-02-08 17:12:55")
> x2 <- as.POSIXct("2006-02-08 17:15:26")
> x2-x1
Time difference of 2.516667 mins
> (x2-x1)/2
Time difference of 1.258333 mins
> x1+60*as.numeric((x2-x1)/2)
[1] "2006-02-08 17:14:10 CET"

However, this goes wrong (although not without warning):
> x1+(x2-x1)/2
[1] "2006-02-08 17:12:56 CET"
Warning message:
Incompatible methods ("+.POSIXt", "Ops.difftime") for "+"

---for reasons that are related to this:
> as.numeric(x2)-as.numeric(x1) # seconds!
[1] 151
> as.numeric(x2-x1) # minutes
[1] 2.516667

(This suggests to me that the methodology for difftime objects is
still not quite complete. Volunteers?)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list