determining which id's have a time point that's earlier than another time point in R

I am trying to find a way to return subject_ids with time_2 that’s earlier than time_1

subject_id Time_1 Time_2
191-5467 18:00 20:00
191-6784 18:30 18:50
191-3457 19:00 21:45
191-0987 19:30 20:00
191-1245 19:45 19:00
191-2365 20:00 19:00

So in this case the code would return subject id’s 191-1245 and 191-2365 because both their time_2 is earlier than time_1.

>Solution :

A dplyr solution:

library(dplyr)
df %>% 
  filter(strptime(Time_2, '%H:%M') < strptime(Time_1, '%H:%M')) %>% 
  pull(subject_id)
[1] "191-1245" "191-2365"

Leave a Reply