This is probably one case where you want to use the 'difftime' function
directly:

> 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
> x3 <- x2-x1
> str(x3)
Class 'difftime'  atomic [1:1] 2.52
  ..- attr(*, "tzone")= chr ""
  ..- attr(*, "units")= chr "mins"
> x4 <- difftime(x2,x1,units='sec')
> str(x4)
Class 'difftime'  atomic [1:1] 151
  ..- attr(*, "tzone")= chr ""
  ..- attr(*, "units")= chr "secs"
>



On 20 Jun 2006 12:56:07 +0200, Peter Dalgaard <p.dalgaard@biostat.ku.dk>
wrote:
>
> "COMTE Guillaume" <g.comte@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@biostat.ku.dk)                  FAX: (+45)
> 35327907
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

