Bug 16573 - vector with date objects does not retain class if the vector begins with NA
Summary: vector with date objects does not retain class if the vector begins with NA
Status: UNCONFIRMED
Alias: None
Product: R
Classification: Unclassified
Component: Language (show other bugs)
Version: R 3.2.2
Hardware: All All
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2015-10-19 17:12 UTC by Gene Leynes
Modified: 2015-10-19 20:35 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gene Leynes 2015-10-19 17:12:21 UTC
Creating a vector with date objects will lose it's class if the first element of the vector is NA.

> mydate <- as.POSIXct("2013-10-09")
> 
> ## I would expect this to be POSIXct, and it is:
> str(c(mydate, NA))
 POSIXct[1:2], format: "2013-10-09" NA
> 
> ## I would expect this to be POSIXct, but it's not:
> str(c(NA, mydate))
 num [1:2] NA 1.38e+09


Tested on R 3.2.1 (LINUX) and R 3.2.2 (WINDOWS)
Comment 1 Michael Lawrence 2015-10-19 18:00:00 UTC
This breaks since dispatch on 'c' is based on the first argument. Since that is unlikely to be worked out, please explicitly coerce the NA to Date and then combine, like:

> str(c(as.Date(NA), Sys.Date()))
 Date[1:2], format: NA "2015-10-19"

One could argue that the explicit coercion often makes the code easier to understand.
Comment 2 Gene Leynes 2015-10-19 18:27:24 UTC
(In reply to Michael Lawrence from comment #1)
> This breaks since ...
What breaks?
Comment 3 Michael Lawrence 2015-10-19 18:39:12 UTC
I meant that the class is lost, since c() does not dispatch to c.POSIXct but instead it treats POSIXct as numeric (which it is under the covers) and generates a numeric vector. This is because 'c' dispatches on its first argument (in the S3 case only). 

In my last reply, I suggested a workaround for Date, but I realized you were using POSIXct instead. The general strategy is the same.

> class(c(as.POSIXct(NA), Sys.time()))
[1] "POSIXct" "POSIXt"
Comment 4 Gene Leynes 2015-10-19 20:35:42 UTC
(In reply to Michael Lawrence from comment #3)
> In my last reply, I suggested a workaround for Date, but I realized you were
> using POSIXct instead. The general strategy is the same.
Yes, I understood.  Thanks.

> I meant that the class is lost, since c() does not dispatch to c.POSIXct but
> instead it treats POSIXct as numeric (which it is under the covers) and
> generates a numeric vector. This is because 'c' dispatches on its first
> argument (in the S3 case only). 
I see, thanks for the clarification.  

By the way, I missed this (sorry!) in ?c:
> The output type is determined from the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression

I knew most of this from experience, but I never noticed that date objects don't have a place in the hierarchy.  This helps explain some of my observations. 

I know this might not be fixable given the maturity of the POSIXct class, but I didn't see it reported previously.