This builds a date object with as.POSIXct
and adds the duration, then truncates the output to Time with strftime
. Resets to 00:00:00
if it counts higher than 24h. For higher numbers see the approach at the bottom.
df
Duration
1 19
2 250
3 3
4 3600
df$Expected <- strftime(as.POSIXct("00:00:00", format="%H:%M:%S") +
df$Duration, format="%H:%M:%S")
df
Duration Expected
1 19 00:00:19
2 250 00:04:10
3 3 00:00:03
4 3600 01:00:00
In case you need to count higher than a day use this
df
Duration
1 19
2 250
3 3
4 3600
5 431170
df$Expected <- paste0(sprintf("%02.f",floor(df$Duration/3600)),":",
sprintf("%02.f",(df$Duration/60)%%60),":",
sprintf("%02.f",df$Duration%%60))
df
Duration Expected
1 19 00:00:19
2 250 00:04:10
3 3 00:00:03
4 3600 01:00:00
5 431170 119:46:10