Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading