[R] ifelse strips POSIXct class from object

Denis Chabot chabot.denis at gmail.com
Sun Jun 5 14:23:30 CEST 2011


Hi,

I was "losing" my dates in a script and upon inspection, found that my recent switch from separate "if" and "else" to "ifelse" was the cause. But why?

my.date	= as.POSIXct("2011-06-04 08:00:00")
default.date = seq(as.POSIXct("2011-01-01 08:00:00"), as.POSIXct("2011-09-01 08:00:00"), length=15)
x = 4 * 60 * 60
(my.date + x)
(min(default.date) + x)
(new.date = ifelse(!is.na(my.date), my.date + x, min(default.date) + x)	)

(if(!is.na(my.date)) new.date2 = my.date + x  else new.date2= min(default.date) + x	)

On my machine, new.date is "numeric" whereas new.date2 is "POSIXct" and "POSIXt", as desired.

sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
  

Thanks in advance,

Denis


More information about the R-help mailing list