firmo23
firmo23

Reputation: 8404

Convert characters that correspond to time difference to numeric values in minutes

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?

Upvotes: 2

Views: 130

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101373

Try the following base R option using str2lang + gsub

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

Upvotes: 1

akrun
akrun

Reputation: 887128

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

Upvotes: 1

Related Questions