首页 > 解决方案 > R - 如何将十进制转换为 MM:SS.XXX

问题描述

如何使用 R 将 SS.xxx (Seconds.Milliseconds) 转换为 MM:SS.xxx (Minutes:Seconds.Milliseconds)?

例如,我的输入是

time = 92.180

我想要的输出是

time = 01:32.180

所有时间字段都有 3 个小数位。

标签: rduration

解决方案


一个选项是 lubridate 包 - 因为您没有指定输出类,所以我包括了一些可能的输出:

package(lubridate)

t <- 92.180
# your output string as character
lubridate::seconds(t) %>% 
  lubridate::as_datetime() %>% 
  format("%M:%OS3") 

# output as period
lubridate::seconds(t) %>% 
  lubridate::as.period()

# output as duration
lubridate::seconds(t) %>% 
  lubridate::as.duration() 

# output as time time
lubridate::seconds(t) %>% 
  lubridate::as.difftime()

推荐阅读