【问题标题】:Is there a way to convert mm:ss.00 to seconds.00?有没有办法将 mm:ss.00 转换为 seconds.00?
【发布时间】:2012-06-05 20:18:59
【问题描述】:

我有一些 mm:ss.00 格式的性能时间数据(即 02:15.45 或 00:34.58)。 R 将变量识别为一个因素,但我想将每个性能时间转换为几秒钟(即 02:15.45 到 135.45)。我一直在寻找答案,但似乎找不到让它工作的方法。 提前致谢。

【问题讨论】:

  • 如果您的“变量”(我认为它是指数据框中的一列)确实是一个因素,那么您将需要使用as.character() 来获得以下任何方法才能成功。

标签: r time


【解决方案1】:

使用 lubridate 包(tidyverse 的一部分):

library(lubridate)
period_to_seconds(hms("12:12:54"))

【讨论】:

    【解决方案2】:
    library(lubridate)
    df$variable<- hms(df$variable)
    df$variable<- as.numeric(df$variable)
    

    使它成为单线也可以。对我来说就像一个魅力。
    我希望这会有所帮助。

    【讨论】:

    • 本主题的最佳答案
    【解决方案3】:

    您可以使用 Lubridate 包轻松完成此操作。如果您使用格式“h:m:s”,您可以将变量转换为 lubridate 对象

    hms("12:12:54")
    

    然后用

    将其转换为秒
    seconds(hms("12:12:54"))
    

    这里是 JSS 中 lubridate 文章的链接

    http://www.jstatsoft.org/v40/i03/paper

    【讨论】:

    • 这会导致“54S”,这不是我们想要的结果。该问题询问如何将持续时间转换为秒,而不是提取时间对象的秒部分。
    • 使用period_to_seconds 代替seconds
    【解决方案4】:

    这是我使用多年的一个。它也是矢量化的。

    toSeconds <- function(x){
       if (!is.character(x)) stop("x must be a character string of the form H:M:S")
       if (length(x)<=0)return(x)
    
       unlist(
          lapply(x,
             function(i){
                i <- as.numeric(strsplit(i,':',fixed=TRUE)[[1]])
                if (length(i) == 3) 
                   i[1]*3600 + i[2]*60 + i[3]
                else if (length(i) == 2) 
                   i[1]*60 + i[2]
                else if (length(i) == 1) 
                   i[1]
             }  
          )  
       )  
    } 
    

    反之亦然(将小数秒保留为请求的位数:

    secondsToString <- function(x,digits=2){
       unlist(
          lapply(x,
             function(i){
                # fractional seconds
                fs <- as.integer(round((i - round(i))*(10^digits)))
                fmt <- ''
                if (i >= 3600)
                   fmt <- '%H:%M:%S'
                else if (i >= 60)
                fmt <- '%M:%S'
                else
                   fmt <- '%OS'
    
                i <- format(as.POSIXct(strptime("0:0:0","%H:%M:%S")) + i, format=fmt)
                if (fs > 0)
                   sub('[0]+$','',paste(i,fs,sep='.'))
                else
                   i
             }
          )
       )
    }
    

    【讨论】:

      【解决方案5】:

      我不太舒服,所以我不知道是否有任何内置函数可用,但我已经制定了这段代码。

      mmss_to_ss <- function  (string)
      {
        mmss <- strsplit (string, ":", T)
        mm <- as.numeric (mmss[[1]][1])
        ss <- as.numeric (mmss[[1]][2])
        return (mm * 60 + ss)
      }
      

      这将接受 mm:ss 格式的时间字符串并返回秒值。可以轻松修改代码以将 hh:mm:ss 转换为秒。

      【讨论】:

        【解决方案6】:

        查看 strptime。具体

        t = "02:15.45"
        (as.numeric(as.POSIXct(strptime(t, format = "%M:%OS"))) - 
            as.numeric(as.POSIXct(strptime("0", format = "%S"))))
        

        这可行,但可能有点尴尬(这样做主要是因为 POSIXct 烦人的自动单位转换...)

        【讨论】:

          猜你喜欢
          • 2018-07-02
          • 2010-11-24
          • 2013-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多