[R] Trouble: Time Difference with difftime
Duncan Mackay
mackay at northnet.com.au
Mon Aug 15 01:43:26 CEST 2011
If you are going use 'numeric' date values further in numeric
calculations sometimes it is advisable to convert to numeric
> x1 <- as.POSIXct(c('2011-08-11 12:00', '2011-08-14 15:15'))
> difftime(x1[2], x1[1], units = 'days')
Time difference of 3.135417 days
> as.numeric(difftime(x1[2], x1[1], units = 'days'))
[1] 3.135417
x2= 3* difftime(x1[2], x1[1], units = 'days')
> class(x2)
[1] "difftime"
x2= 3* as.numeric(difftime(x1[2], x1[1], units = 'days'))
> class(x2)
[1] "numeric"
Regards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
ARMIDALE NSW 2351
Email: home mackay at northnet.com.au
At 09:31 15/08/2011, you wrote:
>You were using difftime incorrectly; look at the help page.
>
> > x1 <- as.POSIXct(c('2011-08-11 12:00', '2011-08-14 15:15'))
> > x1
>[1] "2011-08-11 12:00:00 EDT" "2011-08-14 15:15:00 EDT"
> > ceiling(difftime(x1[2], x1[1], units = 'days'))
>Time difference of 4 days
> >
>
>
>On Sun, Aug 14, 2011 at 9:33 AM, Jim Trabas <jim.trabas at googlemail.com> 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.
> >
>
>
>
>--
>Jim Holtman
>Data Munger Guru
>
>What is the problem that you are trying to solve?
>
>______________________________________________
>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