72o
72o

Reputation: 43

Is there a function in R that can convert a "time" factor (00:00:00) into seconds

Is there a function in R that can convert a "time" factor into seconds?

Time <- tibble(ID = 1:4, Time = c("00:03:36", "00:02:14", "01:10:25", "00:32:10"))

Upvotes: 0

Views: 153

Answers (2)

d.b
d.b

Reputation: 32558

library(lubridate)
library(dplyr)
Time %>% mutate(secs = period_to_seconds(hms(Time)))
## A tibble: 4 x 3
#     ID Time      secs
#  <int> <chr>    <dbl>
#1     1 00:03:36   216
#2     2 00:02:14   134
#3     3 01:10:25  4225
#4     4 00:32:10  1930

Upvotes: 2

akrun
akrun

Reputation: 887971

An option with as.ITime

library(data.table)
as.numeric(as.ITime(Time$Time) )
#[1]  216  134 4225 1930

Upvotes: 0

Related Questions