[R] c() and dates

David Winsemius dwinsemius at comcast.net
Fri Oct 3 16:51:56 CEST 2014


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-04"    "2000-05-05"    "2623237-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



More information about the R-help mailing list