Reputation: 13
I am working with a dataset that has time tracked as minutes:seconds (34:15 would be 34 minutes and 15 seconds) and its stored as a character currently. Is there a way to convert this to just minutes so that 34:15 would show up as 35.25?
Upvotes: 1
Views: 32
Reputation: 79338
Solutions might include:
time <- c("4:30","2:20","34:15")
Base R:
c(as.matrix(read.table(text=time, sep=':')) %*% c(1,1/60))
[1] 4.500000 2.333333 34.250000
Lubridate:
as.numeric(lubridate::ms(time))/60
[1] 4.500000 2.333333 34.250000
Upvotes: 0
Reputation: 16876
One way would be split on the colon, convert to numeric, then divide the minutes by 60 to get the decimal.
time <- c("4:30","2:20","34:15")
sapply(strsplit(time,":"),
function(x) {
x <- as.numeric(x)
x[1]+x[2]/60
}
)
[1] 4.500000 2.333333 34.250000
Upvotes: 2