将数值转换为时间跨度格式

时间:2019-01-18 17:38:23

标签: r ggplot2

我有几秒钟的值

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

,我想将这些持续时间转换为以下时间跨度格式:hh:mm:ss

我尝试过使用dseconds,但向我展示了

> 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)" 

我该如何实现? thx高级

1 个答案:

答案 0 :(得分:3)

我们可以手动计算所有三个部分,类似于this答案:

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" 

一小时为3600秒,因此x %/% 3600给出了整小时数,(x %% 3600) %/% 60给出了剩余时间的整分钟数,而x %% 60是剩余的秒数