Logical statement comparing time and AI

I’ve asked a similar question before, but it remains unresolved.


I’d like to make a column of data, ‘o2_good’. Where time(as.POSIXct) is => YYYY:MM:DD HH:MM:ss I would populate ‘o2_good’ with TRUE, else FALSE.

I’ve tried to do this using mutate, case when, and all kinds of variations on what seems pretty straightforward but none have worked with the time values (although they will work with character data). I tried specifying time ‘as.POSIXct’ and that did not help.

So I tried AI, which generated:
mutate(condition = if_else(dt_time >= as.POSIXct(“2025-03-20 17:29:41”) & time >= as.hms(“17:29:41”), TRUE, FALSE))

Made me feel better that failed too (see attached). Thanks in advance for any tips on using logical statements with time.

Hi Rob,

Can you elaborate on this more?

My understanding is that you have data with a column that has Date/Time values and want to compare with current time, returns TRUE/FALSE based on the result.

If that’s the case, does this something like this work?

dt_time >= now()

Hi Kan. The idea is to fill the ‘o2_good’ column with either “TRUE” or “FALSE” based on time ‘dt_time’:
if_else(dt_time >= as.POSIXct(“2025-03-20 17:29:41”) & time >= as.hms(“17:29:41”), TRUE, FALSE)
is convoluted but I have also tried
if_else(dt_time >= “2020-03-20 17:29:41”, TRUE, FALSE)
and I have tried
case_when(
dt_time >= ymd(“2025-03-20 17:29:41”) ~ TRUE,
TRUE ~ FALSE
))
These statements work with character data but don’t work with POSIXct.

Thanks for the quick reply back. Any ideas appreciated!

You can do something like this.

dt_time >= ymd_hms("2016-08-15 17:29:41")

If you just want to return TRUE/FALSE, then you don’t need to use if_else or case_when.