1

I have a lis t of values in seconds

seconds=c(139765, 319817, 524729, 1491,  211831, 27571, 15254,  427546, 314016, 9372)

and I want to convert those durations in a timespan format like: hh:mm:ss

I've tried with dseconds, but shows me:

> dseconds(seconds)
 [1] "139765s (~1.62 days)"   "319817s (~3.7 days)"    "524729s (~6.07 days)"  
 [4] "1491s (~24.85 minutes)" "211831s (~2.45 days)"   "27571s (~7.66 hours)"  
 [7] "15254s (~4.24 hours)"   "427546s (~4.95 days)"   "314016s (~3.63 days)"  
[10] "9372s (~2.6 hours)" 

How can i achieve that? thx in advanced

1 Answers1

3

We may compute all three pieces manually, similarly as in this answer:

sprintf("%02d:%02d:%02d", x %/% 3600, (x %% 3600) %/% 60, x %% 60)
#  [1] "38:49:25"  "88:50:17"  "145:45:29" "00:24:51"  "58:50:31"  "07:39:31" 
#  [7] "04:14:14"  "118:45:46" "87:13:36"  "02:36:12" 

As one hour has 3600 seconds, x %/% 3600 gives the number of full hours, (x %% 3600) %/% 60 gives the number of full minutes in the remaining time, while x %% 60 is the number of remaining seconds.

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
  • Because some of durations are more than 100 hours, I tried to extend this way on order to put a format DD:HH:MM:SS, but I failed: `sprintf("%02d:%02d:%02d:%02d", seconds %/% 25200, (seconds %/% 25200), seconds %/% 3600, seconds %/% 60, seconds %% 60)` – Armando González Díaz Jan 18 '19 at 18:10
  • @ArmandoGonzálezDíaz, in that case your question is an exact duplicate of the one used to close it. And an analogue of my answer becomes a little too lengthy: `sprintf("%02d:%02d:%02d:%02d", x %/% (3600 * 24), (x %% (3600 * 24)) %/% 3600, (x %% 3600) %/% 60, x %% 60)`. – Julius Vainora Jan 18 '19 at 18:16
  • I've achived it: `sprintf("%02d:%02d:%02d:%02d", seconds %/% 86400, (seconds %% 86400) %/% 3600, (seconds %% 3600) %/% 60, seconds %% 60)` – Armando González Díaz Jan 18 '19 at 18:20
  • @ArmandoGonzálezDíaz, yes, that's the same as my comment. – Julius Vainora Jan 18 '19 at 18:21