How do I convert a column from a duration expressed in hhmmss to just seconds?

79 Views Asked by At

I uploaded many csv files to combine into one big table That new table has a column in format hhmmss with time durations, I want to transform that time duration into seconds. I'm stuck on how to do it.

If I use class on that column, it tells me "hms" "difftime" If I try to calculate the mean of that column, I get "Time difference of NA secs" If I try to use period_to_seconds, I get **trying to get slot "minute" from an object (class "hms") that is not an S4 object **

1

There are 1 best solutions below

0
Phil On
library(lubridate)
library(readr)
library(dplyr)

lakers |>
  as_tibble() |> 
  select(time) |> 
  mutate(time = parse_time(time), 
         seconds = seconds(time))

# A tibble: 34,624 × 2
   time   seconds 
   <time> <Period>
 1 12:00  43200S  
 2 11:39  41940S  
 3 11:37  41820S  
 4 11:25  41100S  
 5 11:23  40980S  
 6 11:22  40920S  
 7 11:22  40920S  
 8 11:22  40920S  
 9 11:00  39600S  
10 10:53  39180S