Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert factor into time in R

Tags:

time

dataframe

r

I have a data frame df like:

ID  time 
a    121:24:30
b    130:30:00

The time column is of factor after importing data. I want convert the values of time column into minutes. At first, I have tried:

df$time <- times(df$time)

But I got warning message:

"out of day time entry"

I notice the value in the hour position is more than 24 in my dataset.

So how am I supposed to do now?

Thanks in advance!

like image 263
Demo Avatar asked Dec 06 '25 01:12

Demo


1 Answers

You could use the lubridate package for this.

library(lubridate)
x <- hms(df$time)
(hour(x) * 60) + minute(x) + (second(x) / 60)
# [1] 7284.5 7830.0
like image 162
Rich Scriven Avatar answered Dec 08 '25 15:12

Rich Scriven