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.
CodePudding user response:
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"
CodePudding user response:
We can convert to datetime with POSIXct
and do the subset
subset(df1, as.POSIXct(Time_2, format = "%H:%M") <
as.POSIXct(Time_1, format = "%H:%M") )$subject_id
[1] "191-1245" "191-2365"
Or using data.table
library(data.table)
setDT(df1)[, subject_id[as.ITime(Time_2) < as.ITime(Time_1)]]
[1] "191-1245" "191-2365"
data
df1 <- structure(list(subject_id = c("191-5467", "191-6784", "191-3457",
"191-0987", "191-1245", "191-2365"), Time_1 = c("18:00", "18:30",
"19:00", "19:30", "19:45", "20:00"), Time_2 = c("20:00", "18:50",
"21:45", "20:00", "19:00", "19:00")), class = "data.frame", row.names = c(NA,
-6L))
CodePudding user response:
Similar to the @Jilber Urbina answer, but using lubridate
.
library(lubridate)
library(dplyr)
df1 |>
# Transform Time_1 and Time_2 to hour minutes with hm
mutate(across(starts_with("Time"), hm)) |>
# Filter to condition of interest
filter(Time_2 < Time_1) |>
# Pull the column with the id values
pull(subject_id)