Using dplyr
, can group by 'OriginId' and then get the row index of 'Time' that has the minimum 'Time' with which.min
, extract that row with slice
.
library(dplyr)
df1 %>%
group_by(OriginId) %>%
slice(which.min(Time))
Or, if we consider to use data.table
, convert the 'data.frame' to 'data.table' (setDT(df1)
), grouped by 'OriginId', we get the row index (as in the previous case) and subset the rows of the dataset (.SD
).
library(data.table)
setDT(df1)[, .SD[which.min(Time)], by = OriginId]