[R] c() and dates

2014-10-03 Thread Therneau, Terry M., Ph.D.

I'm a bit puzzled by a certain behavior with dates.  (R version 3.1.1)

 temp1 - as.Date(1:2, origin=2000/5/3)
 temp1
[1] 2000-05-04 2000-05-05

 temp2 - as.POSIXct(temp1)
 temp2
[1] 2000-05-03 19:00:00 CDT 2000-05-04 19:00:00 CDT

So far so good.  On 5/4, midnight in Greenwich it was 19:00 on 5/3 in my time zone.  The 
manual page has a clear explanation of what goes on.


 c(temp1, temp2)
[1] 2000-05-042000-05-052623237-10-15 2623474-05-06
 class(c(temp1, temp2))
[1] Date

 c(temp2, temp1)
[1] 2000-05-03 19:00:00 CDT 2000-05-04 19:00:00 CDT
[3] 1969-12-31 21:04:41 CST 1969-12-31 21:04:42 CST
 class(c(temp2, temp1))
[1] POSIXct POSIXt

I would have expected c() to determine a common class, somehow, then do the conversion and 
concatonate.  That is obviously not what happens.  I've read the manual page but I must be 
missing something.  I make no claim that R is broken, mistaken, or otherwise deficient, 
only that my understanding is so.


Could someone illuminate?

Terry T.

__
R-help@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.


Re: [R] c() and dates

2014-10-03 Thread David Winsemius

On Oct 3, 2014, at 7:19 AM, Therneau, Terry M., Ph.D. wrote:

 I'm a bit puzzled by a certain behavior with dates.  (R version 3.1.1)
 
  temp1 - as.Date(1:2, origin=2000/5/3)
  temp1
 [1] 2000-05-04 2000-05-05
 
  temp2 - as.POSIXct(temp1)
  temp2
 [1] 2000-05-03 19:00:00 CDT 2000-05-04 19:00:00 CDT
 
 So far so good.  On 5/4, midnight in Greenwich it was 19:00 on 5/3 in my time 
 zone.  The manual page has a clear explanation of what goes on.
 
  c(temp1, temp2)
 [1] 2000-05-042000-05-052623237-10-15 2623474-05-06
  class(c(temp1, temp2))
 [1] Date
 
  c(temp2, temp1)
 [1] 2000-05-03 19:00:00 CDT 2000-05-04 19:00:00 CDT
 [3] 1969-12-31 21:04:41 CST 1969-12-31 21:04:42 CST
  class(c(temp2, temp1))
 [1] POSIXct POSIXt
 
 I would have expected c() to determine a common class, somehow, then do the 
 conversion and concatonate.  That is obviously not what happens.  I've read 
 the manual page but I must be missing something.  I make no claim that R is 
 broken, mistaken, or otherwise deficient, only that my understanding is so.
 

It doesn't appear that any check is made on the commonality of class by either 
c.Date (which one would expect to be called when the Date object is the first 
argument) .  or with c.POSIXctt:

 c.Date
function (..., recursive = FALSE) 
structure(c(unlist(lapply(list(...), unclass))), class = Date)
bytecode: 0x10bc1c0e0
environment: namespace:base

 c.POSIXct
function (..., recursive = FALSE) 
.POSIXct(c(unlist(lapply(list(...), unclass
bytecode: 0x10bc0d470
environment: namespace:base

I don't find any description of the behavior of `c.Date` when I go to a help 
page with ?c.Date.

The only description of the action of `c.POSIXct` that I can find in the page 
to which we are sent with ?c.POSIXct says:

Using c on POSIXlt objects converts them to the current time zone, and on 
POSIXct objects drops any tzone attributes (even if they are all marked 
with the same time zone).


 Could someone illuminate?
 

Agree it's unexpected behavior and worthy of a feature request to R Core, but 
until that obvious undesireable behavior is corrected, I guess the user is left 
with the responsibility of converting all objects to a common class.

-- 

David Winsemius
Alameda, CA, USA

__
R-help@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.


Re: [R] c() and dates

2014-10-03 Thread David Winsemius

On Oct 3, 2014, at 7:19 AM, Therneau, Terry M., Ph.D. wrote:

 I'm a bit puzzled by a certain behavior with dates.  (R version 3.1.1)
 
  temp1 - as.Date(1:2, origin=2000/5/3)
  temp1
 [1] 2000-05-04 2000-05-05
 
  temp2 - as.POSIXct(temp1)
  temp2
 [1] 2000-05-03 19:00:00 CDT 2000-05-04 19:00:00 CDT
 
 So far so good.  On 5/4, midnight in Greenwich it was 19:00 on 5/3 in my time 
 zone.  The manual page has a clear explanation of what goes on.
 
  c(temp1, temp2)
 [1] 2000-05-042000-05-052623237-10-15 2623474-05-06
  class(c(temp1, temp2))
 [1] Date
 
  c(temp2, temp1)
 [1] 2000-05-03 19:00:00 CDT 2000-05-04 19:00:00 CDT
 [3] 1969-12-31 21:04:41 CST 1969-12-31 21:04:42 CST
  class(c(temp2, temp1))
 [1] POSIXct POSIXt
 
 I would have expected c() to determine a common class, somehow, then do the 
 conversion and concatonate.  That is obviously not what happens.  I've read 
 the manual page but I must be missing something.  I make no claim that R is 
 broken, mistaken, or otherwise deficient, only that my understanding is so.
 
 Could someone illuminate?

Followup to my earlier post: It's pretty easy to redefine c.Date and c.POSIXct 
to behave in hte manner is expected:

 c.Date - function (..., recursive = FALSE) 
   structure(c(unlist(lapply(list(...), as.Date))), class = Date)
  temp1 - as.Date(1:2, origin=2000/5/3)
  temp1
#[1] 2000-05-04 2000-05-05
  temp2 - as.POSIXct(temp1)

  c(temp1, temp2)
#[1] 2000-05-04 2000-05-05 2000-05-04 2000-05-05
#  class(c(temp1, temp2))
#[1] Date
#  c(temp2, temp1)
[1] 2000-05-03 17:00:00 PDT 2000-05-04 17:00:00 PDT 1969-12-31 19:04:41 
PST
[4] 1969-12-31 19:04:42 PST

 c.POSIXct -  function (..., recursive = FALSE) 
  .POSIXct(c(unlist(lapply(list(...), as.POSIXct


  c(temp1, temp2)
[1] 2000-05-04 2000-05-05 2000-05-04 2000-05-05
  class(c(temp1, temp2))
[1] Date
  c(temp2, temp1)
[1] 2000-05-03 17:00:00 PDT 2000-05-04 17:00:00 PDT 2000-05-03 17:00:00 
PDT
[4] 2000-05-04 17:00:00 PDT


-- 
David Winsemius
Alameda, CA, USA

__
R-help@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.


Re: [R] c() and dates

2014-10-03 Thread Therneau, Terry M., Ph.D.
Well duh -- type c.Date at the command prompt to see what is going on.  I suspected I 
was being dense.


Now that the behaior is clear can I follow up on David W's comment that redfining the 
c.Date function as


structure(c(unlist(lapply(list(...), as.Date))), class = Date)

allows for a more intellegent response, since it allows all of the as.Date machinery to be 
brought into play.


It seems like a good idea in general.  Would it be a good exchange between the current 
nonsense result, no warning and the new error messages that would arise, e.g., from 
c(as.Date(2000/10/1), factor('b')).




Terry T.

On 10/03/2014 09:52 AM, peter dalgaard wrote:

S3 only has single dispatch, so in one case it dispatches to c.Date and in the 
other to c.POSIXct, both of those return an object of the corresponding class. 
In both cases, the arguments pass through

c(unlist(lapply(list(...), unclass)))

which doesn't look at the class at all. Since Date objects unclass to days and 
POSIXct to seconds, something is bound to go wrong.


__
R-help@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.