R中不规则格式的解析时间

Parse Time of irregular format in R

我有一段时间了,

  [1] "9.58"      "19.19"     "43.03"     "1:40.91"   "2:11.96"   "3:26.00"  
  [7] "3:43.13"   "4:44.79"   "7:20.67"   "12:37.35"  "26:17.53"  "26:44"    

其中一些只有十进制的秒数。其中一些有分钟和小时,并以“:”分隔。

我希望所有这些都在一个单位中(秒或分钟或小时)。我如何在 R

中执行此操作

您可以在冒号分隔符 : 上使用 str_split 拆分字符串并将其转换为秒数。

have <- c("9.58","1:40.91","1:01:02.1")

have_split <- strsplit(have,":")   ## List of times split

convert <- function(x){
    x <- as.numeric(x)
    if(length(x) == 1){               ## Has only seconds
        x                           
    } else if(length(x) == 2){        ## Has seconds and minutes
        out <- x[1]*60+x[2]
    } else if(length(x) == 3){        ## Has seconds, minutes and hours
        out <- x[1]*60^2+x[2]*60+x[3]
    }
}

sapply(have_split,convert)
## [1]    9.58  100.91 3662.10

我总是非常不愿意手动解析日期和时间,与构建专用工具的其他人的测试工作相比,我更不相信自己的代码。

所以我会使用 lubridate 例如:

library(lubridate)

data <-
  c("9.58", "19.19", "43.03", "1:40.91", "2:11.96", "3:26.00", 
   "3:43.13", "4:44.79", "7:20.67", "12:37.35", "26:17.53", "26:44")

difftime(parse_date_time(data, orders = c("%H %M %OS", "%M %OS", "%OS")), 
         parse_date_time("0", orders = "%S"))

# Time differences in secs
#  [1]    9.580002   19.190002   43.029999  100.910004  131.959999
# [6]  206.000000  223.129997  284.790001  440.669998  757.349998
# [11] 1577.529999 1604.000000

lubridate 提供了提供连续尝试的多种解析格式的有利可能性(c("%H:%M:%OS", "%M:%OS", "%OS") 这里,还要注意可以省略 : 分隔符,允许更健壮的解析输入数据格式不正确的情况)。
我的解决方案仍然有点 "hacky" 因为我无法将它们直接解析为 difftime,而是 POSIXct,所以我将它们与 0 进行比较以输出 difftime秒。

使用sub

将格式统一后的几种方法
data1 <- sub("^([^:]+:[^:]+)$", "00:\1", sub("^([0-9]*\.*[0-9]*)$", "00:00:\1", data))

1) 使用 chron -将 'data1' 转换为 times 对象,强制转换为 numeric 并乘以一天中的秒数,即 86400

library(chron)
60*60*24*as.numeric(times(data1))
#[1]    9.58   19.19   43.03  100.91  131.96  206.00
#[7]  223.13  284.79  440.67  757.35 1577.53 1604.00

2) 使用 lubridate 中的 period_to_seconds - 转换为日期时间对象,然后使用 period_to_seconds

将其更改为秒数
library(lubridate)
period_to_seconds(hms(data1))
#[1]    9.58   19.19   43.03  100.91  131.96  206.00
#[7]  223.13  284.79  440.67  757.35 1577.53 1604.00