Home > Software design >  Convert characters that correspond to time difference to numeric values in minutes
Convert characters that correspond to time difference to numeric values in minutes

Time:02-01

Let's say I have a vector of characters that correspond calculated time like:

calc<-c("1:17","0:46","11:25") and I want to convert it to numeric values of minutes like calc<-c(77,46,685) . Is there a way to do it?

CodePudding user response:

We may use ms (minutes:seconds) from lubridate to convert to period class and then convert the period to seconds with period_to_seconds

lubridate)
period_to_seconds(ms(calc))
#[1]  77  46 685

CodePudding user response:

Try the following base R option using str2lang gsub

> eval(str2lang(sprintf("c(%s)", toString(gsub(":", "*60 ", calc)))))
[1]  77  46 685
  •  Tags:  
  • Related