【发布时间】:2016-08-29 09:17:36
【问题描述】:
尝试将以下持续时间转换为秒
x <- "1005d 16h 09m 57s"
x1 <- "16h 09m 57s"
x2 <- "06h 09m 57s"
x3 <- "09m 57s"
x4 <- "57s"
我已在这篇帖子 Convert factor of format Hh Mm Ss to time duration 中修改了 Jthorpe 的答案。
days <- as.numeric(gsub('^*([0-9]+)d.*$','\\1',x3))
hours <- as.numeric(gsub('^.*([0-9][0-9])h.*$','\\1',x3))
minutes <- as.numeric(gsub('^.*([0-9][0-9])m.*$','\\1',x4))
seconds <- as.numeric(gsub('^.*([0-9][0-9])s.*$','\\1',x4))
duration_seconds <- seconds + 60*minutes + 60*60*hours + 24*60*60*days
但是,这仅适用于 x,而不适用于 x1-x4。现在,我知道我可能可以使用 if 逻辑来解决这个问题,但有没有更好的方法?
提前致谢。
【问题讨论】: