[R] Trouble: Time Difference with difftime

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Aug 14 20:20:30 CEST 2011


Use Date instead of POSIXct. Since your data is already POSIXct you need to 
convert to character and then Date before you use difftime.

 > a <- as.POSIXct("2011-08-02 00:01:00")
 > b <- as.POSIXct("2011-08-01 23:59:00")
 > 
as.Date(as.character(a,format="%Y-%m-%d"))-as.Date(as.character(b,format="%Y-%m-%d"))

On 08/14/2011 06:33 AM, Jim Trabas wrote:
> Hello all!!!
>
> I want to measure the duration of events (given a start and an end time).
> The catch is that I require the output in calender days. This means:
>
> 02-Jan-2011 00:01:00  minus 01-Jan-2011 23:59:00 should be 1 day (although
> the real time difference is only 2 minutes)
>
>
> My data is the following
>
> head(episode.ct)
> [1] "2009-07-13 13:37:20 CEST" "2009-07-14 07:29:20 CEST" "2009-07-14
> 07:51:20 CEST" "2009-07-14 08:45:20 CEST"
> [5] "2009-07-14 09:43:20 CEST" "2009-07-14 10:01:20 CEST"
>
> head(episode.end.ct)
> [1] "2009-07-13 13:47:20 CEST" "2009-07-14 07:49:20 CEST" "2009-07-14
> 08:01:20 CEST" "2009-07-14 08:53:20 CEST"
> [5] "2009-07-14 09:51:20 CEST" "2009-07-14 10:11:20 CEST"
>
> attributes(episode.ct)
> $class
> [1] "POSIXct" "POSIXt"
>
> $tzone
> [1] ""
>
> attributes(episode.end.ct)
> $class
> [1] "POSIXct" "POSIXt"
>
> $tzone
> [1] ""
>
>
> When I try :
>
>> difftime(episode.ct-episode.end.ct)
> I get
>
> Error in as.POSIXct.default(time1) :
>    do not know how to convert 'time1' to class "POSIXct"
>
>
> Why is that? Is difftime what I need?
>
>
> Thank you very much
> JT
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Trouble-Time-Difference-with-difftime-tp3742847p3742847.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list