R 将变量字符串格式解析为持续时间

问题描述 投票:0回答:2

我有一个简单的字符串列表,我想将其转换为总秒数的格式。

示例列表:

B = c("39.2s", "31m 18s", "9ms")

我尝试了

as.duration
,但是“m”字符被解析为月份:
as.duration(B) [1] "39.2s"                    "81523818s (~2.58 years)"  "23668201s (~39.13 weeks)" 

我可以尝试什么才能使字符串的不同格式始终解析为秒?

r lubridate
2个回答
0
投票

这是一个可能的解决方案。您可以将所有内容都转换为秒,然后将具有多个单位的内容相加。然后,将秒值转换为持续时间。这是一个执行此操作的函数。输出是由

lubridate::as.duration()
构成的持续时间向量。

B = c("39.2s", "31m 18s", "9ms")

to_seconds <- function(x){
  require(stringr)
  ## identify the multiple for minuts, seconds and miliseconds
  mults <- c("m" = 60, "s" = 1, "ms" = 1/1000)
  ## identify all numbers in the string
  vals <- str_extract_all(x, "[\\d\\.]*")
  ## identify all non-numbers in the string (i.e., the units)
  units <- str_extract_all(x, "[a-z]*")
  ## turn the numeric values into numbers
  vals <- lapply(vals, function(x)as.numeric(x[-which(x == "")]))
  ## removing missing values in units
  units <- lapply(units, function(x)x[-which(x == "")])
  ## turn everything into seconds and sum
  secs <- sapply(seq_along(vals), function(x){
    unname(sum(vals[[x]]*mults[units[[x]]]))
    })
  ## turn into duration
  lubridate::as.duration(paste0(secs, "s"))
}
to_seconds(B)
#> Loading required package: stringr
#> [1] "39.2s"                 "1878s (~31.3 minutes)" "0.009s"

创建于 2023-08-10,使用 reprex v2.0.2


0
投票

我认为没有毫秒标记,这意味着这些值需要被视为秒并除以 1000。如果字符串中没有月份,则所有

m
值都可以被视为分钟,您可以做:

library(lubridate)
library(stringr)

duration(
  toupper(
    str_replace(
      str_replace(B, "\\d+(?=ms)", \(x) as.character(as.numeric(x) / 1000)),
      "ms", "s")
    )
  )

[1] "39.2s"                 "1878s (~31.3 minutes)" "0.009s"  
© www.soinside.com 2019 - 2024. All rights reserved.