mercredi 28 juin 2017

unable to retrieve POSIXct from numeric format after ifelse statement (R)

I have a table like so:

dtab<-data.table(Arr.Dep=c("A","D"),
       time=c("2017-05-01 04:50:00","2017-05-01 04:55:00"))

dtab[,time:=parse_date_time(dtab$time, c("%y-%m-%d %H:%M%S"))]

Operations on the date and time column seem successful:

dtab[,time2:=time+2]

But if I try to run an ifelse statement, the POSIXct format goes back to numeric and I seem to be unable to bring it back to date and time.

dtab[,time3:=ifelse(dtab[,Arr.Dep]=="A",dtab[,time]+2,"hello")]

I saw the issue has already been raised: R- date time variable loses format after ifelse

Unfortunately it's not of great help to me, as when I try to follow the example - adding 2 seconds rather than replacing with NA as in the OP -, I hit an error anyway. Any help?

Aucun commentaire:

Enregistrer un commentaire