以亚秒级解析时间数据

Parse time data with subseconds

我有一些时间数据

01:09:00
00:14:00
00:00:00
11:47:00
10:34:00
08:15:00

数据以%M:%S:00计算(第一个数字是分钟,第二个数字是秒)。我想将其转换为总秒数。使用 lubridate 很容易做到这一点,但 R 一直认为格式在 %H:%M:%S 中。

lubridate 能否以我的数据所采用的格式计算经过的总秒数?如果不是,将数据转换为适当格式的最佳方法是什么?

我想过转换成字符,拼接出分秒。

library(lubridate)
foo = function(x){
    hms(sapply(strsplit(x, ":"), function(xx) paste("01", xx[1], xx[2], sep = ":")))
}
a = "01:09:00"
b = "00:14:00"
foo(a) - foo(b)
#[1] "1M -5S"

#OR
as.period(foo(a) - foo(b), unit = "secs")
#[1] "55S"

也许下面会做。

NumSeconds <- function(x){
    f <- function(y)
        sum(sapply(strsplit(y, split=":"), as.numeric) * c(60, 1, 0))
    unname(sapply(x, f))
}

x <- scan(what = "character", text = "
01:09:00
00:14:00
00:00:00
11:47:00
10:34:00
08:15:00")

NumSeconds(x)
[1]  69  14   0 707 634 495

您可以使用 data.table::as.ITime 并将格式指定为 "%M:%S"*:

x <- c("01:09:00", "10:34:00")
as.integer(as.ITime(x, format = "%M:%S"))
# [1]  69 634

*format 参数传递给 strptime 并且...

Each input string is processed as far as necessary for the format specified: any trailing characters are ignored. [...] Note that %S does not read fractional parts on output.


或者,最有可能更快,substr

as.integer(substr(x, 1, 2)) * 60 + as.integer(substr(x, 4, 5))
# [1]  69 634