有没有办法将记录为分钟:秒的时间转换为数字形式



我正在使用一个数据集,该数据集的时间跟踪为分钟:秒(34:15将为34分15秒(,并且当前存储为字符。有没有办法将其转换为分钟,使34:15显示为35.25?

一种方法是在冒号上拆分,转换为数字,然后将分钟除以60得到小数。

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

解决方案可能包括:

time <- c("4:30","2:20","34:15")

基本R:

c(as.matrix(read.table(text=time, sep=':')) %*% c(1,1/60))
[1]  4.500000  2.333333 34.250000

润滑脂:

as.numeric(lubridate::ms(time))/60
[1]  4.500000  2.333333 34.250000

最新更新