[Rd] ifelse drops classes

Hilmar Berger hilmar.berger at imise.uni-leipzig.de
Fri Feb 29 13:41:31 CET 2008


Hi all,

I guess that this is rather a feature request than a bug report, but I'm 
not really sure:

I stumbled over this today (R 2.6.2, WinXP):

 > c=c(as.Date("2007-01-01"))
 > class(c)
[1] "Date"
 > ifelse(is.na(c),as.Date(Sys.time()), c)
[1] 13514
 > typeof(ifelse(is.na(c),as.Date(Sys.time()), c))
[1] "double"
 > class(ifelse(is.na(c),as.Date(Sys.time()), c))
[1] "numeric"
 > mode(ifelse(is.na(c),as.Date(Sys.time()), c))
[1] "numeric"

So - unexpected by me - ifelse drops the date class.

Afterwards I found in the Help page:

The mode of the result may depend on the value of test, and the class 
attribute of the result is taken from test and may be inappropriate for 
the values selected from yes and no.

So even I should expect that the class of the results might not "Date",

1. shouldn't it be 'logical' (class(TRUE)) instead of 'numeric' ?
2. Isn't it pretty useless to take the class from the test argument, 
given the fact that this will be usely class(TRUE/FALSE) ?
3. If we can take the mode from the value selected, why can't we take 
the class attribute as well ?

The Help page advices:
Sometimes it is better to use a construction such as (tmp <- yes; 
tmp[!test] <- no[!test]; tmp), possibly extended to handle missing 
values in test.

So - why doesn't ifelse use a similiar construction ?

In conclusion, I plead for changing ifelse() to not drop class attributes.

Regards,
Hilmar



More information about the R-devel mailing list