mardi 23 juin 2020

How can I alter the date of a datetime vector depending on the time with lubridate?

I am trying to manipulate a date inside a datetime vector depending on time of day.

Each item in the vector newmagic looks something like this "2020-03-05 02:03:54 UTC" For all the items that have a time between 19:00 and 23:59 I want to go back one day.

I tried writing an if statement:

if(hour(newmagic)>=19&hour(newmagic)<=23){
  date(newmagic)<-date(newmagic)-1
}

giving me no output but

Warning message: In if (hour(newmagic) >= 19 & hour(newmagic) <= 23) { : the condition has length > 1 and only the first element will be used

when I limit the data to the condition and simply execute date()-1

newmagic[hour(newmagic)>=19&hour(newmagic)<=23&!is.na(newmagic)] <- date(newmagic[hour(newmagic)>=19&hour(newmagic)<=23&!is.na(newmagic)])-1

The output does remove 1 day but also sets the time to 0 Original:

"2020-03-07 20:58:00 UTC"

After date()-1

"2020-03-06 00:00:00 UTC"

I don't really know how to go on. How can I adapt the if statement so that it will actually do what I intend to? How can I rewrite the limitation in the second approach so that the time itself will stay intact?

Thank you for the help

Aucun commentaire:

Enregistrer un commentaire