dukes00
dukes00

Reputation: 57

Converting time in seconds to Hours:Minutes:Seconds

I'm currently working on data about an ultramarathon. There's many checkpoints with timestamps for each runner's time on that checkpoint, but as I'm trying to study the split times I need to calculate the half times, which is not a problem in itself, but I'm having issues with formatting.

The data I scraped looks like this:

# A tibble: 397 x 15
   General Name  `12`     `1`    `2`    `3`      `4`      `5`      `6`      `7`      `8`      `9`      `10`     `11`    
     <dbl> <chr> <time>   <time> <time> <time>   <time>   <time>   <time>   <time>   <time>   <time>   <time>   <time>  
 1       2 Chri… 10:02:17 00'00" 56'27" 01:36:58 03:23:54 05:05:31 05:53:18 06:17:25 06:54:33 07:33:36 08:22:16 08:55:23
 2       3 Nico… 10:14:57 00'00" 55'47" 01:35:28 03:22:24 05:16:22 06:09:51 06:33:52 07:11:12 07:50:28 08:38:20 09:10:06
 3       4 Maxi… 10:32:46 00'00" 55'42" 01:35:08 03:23:01 05:13:12 06:06:35 06:32:58 07:13:10 07:58:11 08:47:50 09:22:07
 4       5 Diog… 10:42:56 00'00" 58'50" 01:42:46 03:34:29 05:36:03 06:29:57 06:54:23 07:35:31 08:14:50 09:03:12 09:38:53
 5       6 Migu… 10:44:52 00'00" 56'27" 01:38:00 03:23:53 05:15:39 06:10:39 06:37:54 07:22:36 08:04:20 08:58:35 09:38:21
 6       7 Jero… 10:52:52 00'00" 55'47" 01:35:06 03:21:35 05:20:53 06:16:44 06:43:40 07:24:17 08:11:34 09:03:10 09:37:00
 7       8 Aroa… 10:55:36 00'00" 59'28" 01:44:40 03:40:16 05:38:53 06:33:51 07:01:58 07:44:36 08:27:52 09:17:12 09:49:58
 8       9 Didi… 10:59:40 00'00" 59'28" 01:47:58 03:44:40 05:44:27 06:38:32 07:03:09 07:45:02 08:26:12 09:17:07 09:52:00
 9      10 Ales… 11:01:28 00'00" 56'50" 01:37:40 03:28:33 05:20:52 06:15:39 06:42:38 07:27:43 08:07:42 09:01:04 09:43:31
10      11 Davi… 11:02:49 00'00" 57'08" 01:38:15 03:28:47 05:19:02 06:13:10 06:40:44 07:24:51 08:09:10 09:06:07 09:45:14

(The current timestamp formatting is what I'm trying to preserve.)

I think the issue is with number rounding, as I calculate the half time by doing this:

miut_joined <- miut_joined %>%
  drop_na() %>%
  mutate(`6.5` = (`5`+((`6`-`5`)/5.6)*2.5))

(The 5.6 and 2.5 are specific distances between existing checkpoints and between the first of those two checkpoints and the half mark.) And the results don't always come out as whole numbers.

When I do this the data I get looks like this:

 miut_melted %>% select(-Name) %>% filter(Checkpoint == 6.5)
# A tibble: 397 x 3
   General Checkpoint  Time
     <dbl> <chr>      <dbl>
 1       2 6.5        19611
 2       3 6.5        20417
 3       4 6.5        20222
 4       5 6.5        21608
 5       6 6.5        20414
 6       7 6.5        20751
 7       8 6.5        21806
 8       9 6.5        22117
 9      10 6.5        20720
10      11 6.5        20592
# … with 387 more rows

I tried playing around with lubridate::seconds_to_period but the resulting data doesn't play nice with ggplot2.

How do I correctly convert time in seconds to Hours:Minutes:Seconds?

Upvotes: 4

Views: 864

Answers (2)

Achal Neupane
Achal Neupane

Reputation: 5719

You could do with lubridate package:

total_secs <- 100000
new_time <- seconds_to_period(total_secs)
sprintf('%02d:%02d:%02d', new_time@hour, minute(new_time), second(new_time))
# [1] "03:46:40"

Upvotes: 2

G. Grothendieck
G. Grothendieck

Reputation: 269664

If the problem is to take a vector of numbers representing seconds and convert each to HH:MM:SS then the chron times class can be used. It represents times as fraction of a day internally so:

library(chron)

secs <- c(100, 60, 30) # test input

tt <- times(secs / (24 * 60 * 60))
tt
## [1] 00:01:40 00:01:00 00:00:30

format(tt)
## [1] "00:01:40" "00:01:00" "00:00:30"

hours(tt)
## [1] 0 0 0

minutes(tt)
## [1] 1 1 0

seconds(tt)
## [1] 40  0 30

# add 30 seconds to each time
tt + times("00:00:30")
## [1] 00:02:10 00:01:30 00:01:00

# calculate differences in seconds, tt[i] - tt[i-1] for i = 2, 3, ...
24 * 60 * 60 * diff(tt)
## [1] -40 -30

Upvotes: 1

Related Questions