[R] ifelse strips POSIXct class from object

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 5 14:48:52 CEST 2011


On Sun, Jun 5, 2011 at 8:23 AM, Denis Chabot <chabot.denis at gmail.com> wrote:
> 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) )
>

Try replace:

new.date <- replace(my.date, is.na(my.date), min(default.date)) + x


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list