Abhinav Rawat Abhinav Rawat - 2 months ago 34
R Question

How to get max value of lubridate time object from a variable of a data frame in R

I have a data frame which includes timed data, the time column includes object of

lubridate::hms()
, now how can get the max time object value from the variable time in R.

Snapshot of the data frame
I was trying do it by this basic approach, but it returns value in double format->

> max(comdty1$Time)
[1] 59.99531


also this is fine

> max(lubridate::hour(comdty1$Time))
[1] 17


TIA

Answer Source

This is the solution I came up with

# get the maximum hour
maxHour1 <- max(hour(comdty1$Time))
# get the corresponding complete time object
tail(comdty1$Time[which(hour(comdty1$Time)==TmaxHour)],1)