[R] POSIXct: subtle difference in representation leads to error for "+"

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed May 21 10:06:16 CEST 2014


On 21/05/2014 08:33, Jeff Newmiller wrote:
> You did not show us how "x" was created, so we cannot tell whether this indicates a bug or not. My bet would be that "x' was created in some way that bypassed the standard POSIXct creation code.
>
> Note that your timezone specification is not atypical for output shorthand but may not yield consistent results for input on different OS or locale combinations. I think "Etc/GMT-1" or "Europe/Amsterdam" will be more reliable depending on your typical data sources.

Etc/GMT-1 is not standard (although widely implemented) and does not 
have DST so seems inappropriate here.  But note that we only have the 
time-zone abbreviations here, not the 'timezone specification'.  (See 
?Sys.timezone.)

"CET" is pretty much portable as a time zone and well-defined post 1945. 
But that does not mean that many places in the current CET time zone 
have used CET ever since 1945: the DST transition rules were changing up 
to the 1980s, and Portugal has been in and out of CET (twice) since.

All systems I know will use CET and CEST consistently as abbreviations 
for  UTC+01:00 and UTC+02:00 (with the sign commonly used for that 
notation, the opposite of GMT-1).

> ---------------------------------------------------------------------------
> 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.
>
> On May 20, 2014 11:11:12 PM PDT, Jens Scheidtmann <jens.scheidtmann at gmail.com> wrote:
>> Dear all,
>>
>> I have had a "weird" problem in R 3.0.2:
>>
>>> x
>> [1] "2006-03-14 12:48:01 CET"  "2006-05-02 11:09:48 CEST"
>>> str(x)
>> POSIXct[1:2], format: "2006-03-14 12:48:01" "2006-05-02 11:09:48"
>>> x + 6
>> Error in unclass(e1) + unclass(e2) :
>>   non-numeric argument to binary operator
>>> as.POSIXct(x) + 6
>> Error in unclass(e1) + unclass(e2) :
>>   non-numeric argument to binary operator
>>
>> I was puzzled to the max and did not understand what's wrong:
>>
>>> y <- c(as.POSIXct("2006-03-14 12:48:01 CET"), as.POSIXct("2006-05-02
>> 11:09:48 CEST"))
>>> y
>> [1] "2006-03-14 12:48:01 CET"  "2006-05-02 11:09:48 CEST"
>>> str(y)
>> POSIXct[1:2], format: "2006-03-14 12:48:01" "2006-05-02 11:09:48"
>>> y + 6
>> [1] "2006-03-14 12:48:07 CET"  "2006-05-02 11:09:54 CEST"
>>
>> My immediate assumption was, that something was wrong with the internal
>> representation. After some googling I discovered dput and there it was:
>>
>>> dput(x)
>> structure(c("1142336881", "1146560988"), class = c("POSIXct",
>> "POSIXt"))
>>> dput(y)
>> structure(c(1142336881, 1146560988), class = c("POSIXct", "POSIXt"))
>>
>> So on the inside "x" is a character vector, while "y" is a numeric
>> vector.
>> Ok, seems I need to force conversion to the "sensible" representation:
>>
>>> xx <- as.POSIXct(as.character(x))
>>> dput(xx)
>> structure(c(1142336881, 1146560988), class = c("POSIXct", "POSIXt"),
>> tzone
>> = "")
>>
>> Now my questions:
>>
>> Shouldn't as.POSIXct() force the recommended or canonical internal
>> representation?
>> Would you regard this as a bug?
>> Or is the code, which created x in the first place to blame?
>>
>> Thanks in advance,
>>
>> Jens
>>
>> 	[[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.
>
> ______________________________________________
> 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.
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list