2014-12-05 117 views

回答

33

您可以尝试

library(lubridate) 
seconds_to_period(86400) 
#[1] "1d 0H 0M 0S" 

seconds_to_period(48000) 
#[1] "13H 20M 0S" 

如果需要format

td <- seconds_to_period(86400) 
sprintf('%02d %02d:%02d:%02d', day(td), [email protected], minute(td), second(td)) 
#[1] "01 00:00:00" 

如果它跨越了>99天,

td <- seconds_to_period(1e7) 
sprintf('%03d %02d:%02d:%02d', day(td), [email protected], minute(td), second(td)) 
#[1] "115 17:46:40" 
0

这也适用于使用seconds.to.hmskimisc,虽不像@ak一样优雅运行的回答是:

library(kimisc) 
library(dplyr) 
library(stringr) 

HMS = seconds.to.hms(86400) 

HMS = seconds.to.hms(48000) 

HMS = seconds.to.hms(1e7) 

HMS %>% 
    str_extract("^\\d+") %>% 
    as.numeric() %>% 
    {paste(.%/%24, .%%24)} %>% 
    str_replace(HMS, "^\\d+", .) 

结果:

[1] "1 0:00:00" 

[1] "0 13:20:00" 

[1] "115 17:46:40" 
相关问题