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

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Wed May 21 09:33:10 CEST 2014


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.
---------------------------------------------------------------------------
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.



More information about the R-help mailing list