Is there a way to convert mm:ss.00 to seconds.00?

前端 未结 6 1760
离开以前
离开以前 2020-12-30 03:12

I\'ve got some performance time data in mm:ss.00 format (i.e. 02:15.45, or 00:34.58). R is recognizing the variable as a factor, but I\'d like to convert each performance t

相关标签:
6条回答
  • 2020-12-30 03:43

    Look into strptime. Specifically

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

    This will work, but is possibly a little awkward (doing it this way mostly because of POSIXct's annoying automatic unit conversion...)

    0 讨论(0)
  • 2020-12-30 03:52
    library(lubridate)
    df$variable<- hms(df$variable)
    df$variable<- as.numeric(df$variable)
    

    make it a one-liner is ok as well. Works like a charm for me.
    I hope this helps.

    0 讨论(0)
  • 2020-12-30 03:55

    Here's one I've used for a number of years. It's vectorized, too.

    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]
             }  
          )  
       )  
    } 
    

    And the reverse (preserves fractional seconds to the number of digits requested:

    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
             }
          )
       )
    }
    
    0 讨论(0)
  • 2020-12-30 03:56

    You can do this easily with the Lubridate package. If you use the format "h:m:s" you can convert the variable to a lubridate object with

    hms("12:12:54")
    

    And then convert it to seconds with

    seconds(hms("12:12:54"))
    

    Here is a link to the lubridate article in JSS

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

    0 讨论(0)
  • 2020-12-30 04:02

    I am not that much comfortable so i don't know if there is any builtin function available, but i have worked out this code.

    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)
    }
    

    This will accept a time string in mm:ss format and return second values. The code can be easily modified to convert from hh:mm:ss to seconds also.

    0 讨论(0)
  • 2020-12-30 04:04

    Using lubridate package (part of tidyverse):

    library(lubridate)
    period_to_seconds(hms("12:12:54"))
    
    0 讨论(0)
提交回复
热议问题