[R] date and time coding question

William Dunlap wdunlap at tibco.com
Thu May 16 17:32:21 CEST 2013


> you should think of the result of converting from difftime to
> numeric (using as.numeric) as the opportunity (or rather requirement) to specify what
> time units you want.

Here is an example of the trouble you can have if you do not specify the units
when converting a difftime object to a numeric one.  In this case the conversion
is implicitly done by plot.

The following dataset contains the sunrise times and the time an alarm rings
in local time (US/Pacific) at various times of this year in northwest Washington.

> dput(d)
structure(list(day = structure(c(15706, 15773, 15774, 15841, 
15865), class = "Date"), sunrise = structure(c(1357056060, 1362839700, 
1362925980, 1368707280, 1370779680), class = c("POSIXct", "POSIXt"
), tzone = ""), alarm = structure(c(1357052400, 1362841200, 1362924000, 
1368712800, 1370786400), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("day", 
"sunrise", "alarm"), row.names = c(NA, -5L), class = "data.frame")
> d
         day             sunrise               alarm
1 2013-01-01 2013-01-01 08:01:00 2013-01-01 07:00:00
2 2013-03-09 2013-03-09 06:35:00 2013-03-09 07:00:00
3 2013-03-10 2013-03-10 07:33:00 2013-03-10 07:00:00
4 2013-05-16 2013-05-16 05:28:00 2013-05-16 07:00:00
5 2013-06-09 2013-06-09 05:08:00 2013-06-09 07:00:00

Here is a plot of the alarm time relative to sunrise on those dates
  > with(d, plot(day, alarm-sunrise))
The y axis units appear to be minutes.

Let's highlight 3 of those points:
  > with(d[c(1,3,5),], points(day, alarm-sunrise, cex=1, pch=10, col="blue"))
The blue marks overstrike the original marks, which is what we expect.

Let's highlight the 2 most extreme points
  > with(d[c(1,5),], points(day, alarm-sunrise, cex=2, pch=1, col="red"))
The red marks do not overstrike the original points because the units are
now hours instead of minutes.  You need to explicitly call as.numeric() to
make it right
  > with(d[c(1,5),], points(day, as.numeric(alarm-sunrise, units="mins"), cex=2.5, pch=1, col="orange"))

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Jeff Newmiller
> Sent: Wednesday, May 15, 2013 8:58 PM
> To: MacQueen, Don; Andras Farkas; r-help at r-project.org
> Subject: Re: [R] date and time coding question
> 
> The difference of two POSIXct values is of type difftime. You should not think of difftime
> as having units. Rather, you should think of the result of converting from difftime to
> numeric (using as.numeric) as the opportunity (or rather requirement) to specify what
> time units you want.  If you let R print the difftime object unconverted, it will print with
> whatever units seem appropriate given the magnitude of the difftime.
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
> 
> "MacQueen, Don" <macqueen1 at llnl.gov> wrote:
> 
> >I think you probably want
> >   format='%m/%d/%y %H:%M')
> >(lower case "y")
> >
> >diff() as suggested by Jeff Newmiller is good, except that I don't know
> >how to control the units using diff().
> >
> >## so a method that allows specifying units other than hours would be,
> >for
> >example,
> >
> >datetime <-c("1/1/13 00:00","1/1/13 12:00","1/2/13 00:00","1/2/13
> >12:00")
> >datetime <-as.POSIXct(datetime,format='%m/%d/%y %H:%M')deltas <-
> >difftime(
> >datetime[-1], datetime[-length(datetime)] , units='min')
> >
> >-Don
> 
> ______________________________________________
> 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