[R] Date arithmetic coerces POSIXlt to POSIXct?

MacQueen, Don macqueen1 at llnl.gov
Wed Mar 9 17:16:43 CET 2011


(Just expanding a bit on the previous response)

"weird" is in the eye of the beholder...

A POSIXlt object is actually a list, and subtracting a number from a list
[as in an expression like list(a=1,b=2) - 3] does not make sense. But
POSIXct objects are actually numbers, and adding a number to a number does
make sense. R is fairly generous about automatic conversions, and the
conversion from "lt" to "ct" makes the arithmetic possible.

I suppose one could say that R, seeing a request to subtract a number from
a time, decides that the user really wants to do the subtraction, and
therefore converts the time to a numeric representation first, in order to
then do the subtraction. This is a design decision (and one that I have
found useful).


> unclass(as.POSIXlt("2011-01-23 12:45:45"))
$sec
[1] 45

$min
[1] 45

$hour
[1] 12

$mday
[1] 23

$mon
[1] 0

$year
[1] 111

$wday
[1] 0

$yday
[1] 22

$isdst
[1] 0

attr(,"tzone")
[1] "US/Pacific" "PST"        "PDT"
> 

> unclass(as.POSIXct("2011-01-23 12:45:45"))
[1] 1295815545
attr(,"tzone")
[1] ""



Adding or subtracting a number to or from a POSIXct object is actually a
reasonable thing to do, provided one knows the units in which the POSIXct
object is stored. For example:

> as.POSIXct("2011-01-23 12:45:45") + 1
[1] "2011-01-23 12:45:46 PST"

> as.POSIXct("2011-01-23 12:45:45") + 60
[1] "2011-01-23 12:46:45 PST"


-Don
-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 3/8/11 7:06 PM, "jdnewmil at gmail.com" <jdnewmil at dcn.davis.ca.us> wrote:

>You should not be surprised at any result you obtain by adding an integer
>to a POSIXlt... that is like adding 5 to 3 meters... 5 whats?
>
>Start by using as.difftime to specify your time units on time intervals.
>As it happens, the default unit used by POSIXt types is seconds, and
>POSIXlt is not designed to support arithmetic, so a conversion to POSIXct
>is performed automatically.
>
>Don't forget that POSIXt handles time zones, including daylight savings,
>but data rarely includes the timezone in it, so you ought to configure
>the TZ environment variable before working with the data you have in a
>POSIXt variable. Many people find it easier to use Date or chron types
>for simple tasks.
>
>In any case, it is documented, and it is not a bug.
>-- 
>Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>
>"Buttrey, Samuel (Sam) (CIV)" <buttrey at nps.edu> wrote:
>
>Hi. This feels like a bug to me, or at least an undocumented feature, but
>I thought I'd see what people here thought of it. Consider a POSIXlt
>object like this one: > a <- as.POSIXlt ("2011-01-23 12:45:45") > class
>(a) [1] "POSIXlt" "POSIXt" Fine. Now, if I do some arithmetic on that
>object, the result is converted to POSIXct. > class (a - 360) [1]
>"POSIXct" "POSIXt" Does this seem weird? I'm running R 2.12.1 under
>Windows XP. Thanks, Sam Buttrey     [[alternative HTML version
>deleted]]_____________________________________________
>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.
>
>
>    [[alternative HTML version deleted]]
>
>______________________________________________
>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