[R] Subtracting POSIXct data/times

William Dunlap wdunlap at tibco.com
Tue Jun 15 05:37:48 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of James Rome
> Sent: Monday, June 14, 2010 2:27 PM
> To: Don MacQueen
> Cc: r-help at r-project.org
> Subject: Re: [R] Subtracting POSIXct data/times
> 
> That fixed it. Dumb me. I had assumed that the subtraction of the raw
> POSIXcts would always give the same results.

Subtracting any given two POSIXct values does
give just one value.  However, the format used
to print the values depends on the other values in the
same vector.  It looks like the print routine selects
units such that the smallest item in the vector is bigger
than one of the chosen unit.
   > z <- as.POSIXct("2010-06-14 20:32:02") + 10^(0:7)
   > z-z[1]
   Time differences in secs
   [1]       0       9      99     999    9999   99999  999999
   [8] 9999999
   > z-mean(z[5:6])
   Time differences in hours
   [1]  -15.2775  -15.2750  -15.2500  -15.0000  -12.5000
   [6]   12.5000  262.5000 2762.5000
   > z-mean(z[7:8])
   Time differences in days
   [1] -63.65740 -63.65729 -63.65625 -63.64583 -63.54167
   [6] -62.50000 -52.08333  52.08333

This is analogous to the print routines for numbers
choosing whether or not to use scientific notation
based on the range of the data or chooosing the number
of digits to print based on the number of digits required to
approximate all values in the vector to the desired
accuracy.  Once the format is  chosen it is used for
all the values in the vector.  E.g.,
   > 111^(0:5)
   [1]           1         111       12321     1367631
   [5]   151807041 16850581551
   > 111^(0:6)
   [1] 1.000000e+00 1.110000e+02 1.232100e+04 1.367631e+06
   [5] 1.518070e+08 1.685058e+10 1.870415e+12
or
   > 1/c(1,2,4,8)
   [1] 1.000 0.500 0.250 0.125
   > 1/c(1,2,3,4)
   [1] 1.0000000 0.5000000 0.3333333 0.2500000

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> Thanks,
> Jim
> 
> On 6/14/10 5:22 PM, Don MacQueen wrote:
> > See the help page for the difftime() function, which will 
> tell you how
> > to specify the units of the differences.
> > (when you don't specify, it chooses the units according to 
> some rules)
> >
> > -Don
> >
> > At 4:24 PM -0400 6/14/10, James Rome wrote:
> >> I have two dataframe columns of POXIXct data/times that 
> include seconds.
> >> I got them into this format using for example
> >> zsort$ETA <- as.POSIXct(as.character(zsort$ETA), format="%m/%d/%Y
> >> %H:%M:%S")
> >>
> >> My problem is that when I subtract the two columns, sometimes the
> >> difference is given in seconds, and sometimes it is given 
> in minutes. I
> >> don't care which it is, but I need to know which one I will get.
> >>
> >> DateTime        ETA
> >> 2010-05-16 02:19:56    2010-05-16 03:46:35
> >> ...
> >> Browse[1]> mins = zsort$ETA - zsort$DateTime
> >> Browse[1]> mins
> >> Time differences in hours
> >>  [1] 1.444167 2.685000 3.077222 3.210278 3.248056 3.281944 3.281944
> >> 3.360278 3.360278 3.582778 4.574444 5.506111 5.857778 
> 6.150278 6.150278
> >> 6.243056 6.243889 6.248056 6.248611 6.248611 6.356667
> >> attr(,"tzone")
> >>
> >> But sometimes the answer is in seconds.
> >> # make a column with the minutes before landing
> >>> zsort$MinBeforeLand = zsort$ETA - zsort$DateTime
> >>>  zsort$MinBeforeLand
> >> Time differences in secs
> >>  [1]   -50   136   221   878  1192  2263  3296  3959  4968 
>  5846  8709
> >> 11537 12198 12442 12642 15952 18273 19952 20538
> >>
> >> How do I specify the resultant units?
> >>
> >> Thanks,
> >> Jim Rome
> >>
> >> ______________________________________________
> >> 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.
> >
> >
> 
> ______________________________________________
> 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