在R中工作超过24小时

alp*_*plv 5 duration r time-format

我有一系列持续时间,最长可达118小时,格式如此"118:34:42",其中118小时,34小时,42小时.输出应该是几秒钟.

我想将它转换为R中的某种时间类型,但我看过的大多数库都想添加一个日期(lubridate,zoo,xts),或者返回"NA",因为时间超出了24小时不等.我可以解析字符串并返回几秒钟,但我想知道是否有更快的方法.

我对R来说有点新手(可能还有3个月的工作时间).

任何帮助搞清楚如何处理这一点将不胜感激.

例:

    library(lubridate)
    x <- c("118:34:42", "114:12:12")
    tt <- hms(x)
    Error in parse_date_time(hms, orders, truncated = truncated, quiet = TRUE) : 
  No formats could be infered from the training set.
    #try another route
    w <- "118:34:42"
    tt2 <- hms(w)
    tt2
    #[1] NA
    z <- "7:02:02"
    tt3 <- hmw(z)
    tt3
    #[1] "7H 2M 2S"
Run Code Online (Sandbox Code Playgroud)

And*_*rie 7

lubridate包中有一个hms()返回时间对象的函数:

library(lubridate)

x <- c("118:34:42", "114:12:12")
tt <- hms(x)

tt
[1] 118 hours, 34 minutes and 42 seconds 
[2] 114 hours, 12 minutes and 12 seconds 
Run Code Online (Sandbox Code Playgroud)

该函数hms()返回类的对象Period:

str(tt)
Formal class 'Period' [package "lubridate"] with 6 slots
  ..@ .Data : num [1:2] 42 12
  ..@ year  : num [1:2] 0 0
  ..@ month : num [1:2] 0 0
  ..@ day   : num [1:2] 0 0
  ..@ hour  : num [1:2] 118 114
  ..@ minute: num [1:2] 34 12
Run Code Online (Sandbox Code Playgroud)

您可以使用这些对象进行算术运算.例如:

tt[2] - tt[1]
[1] -4 hours, -22 minutes and -30 seconds 
Run Code Online (Sandbox Code Playgroud)