将小时:分钟:秒转换为分钟 [英] Convert hours:minutes:seconds to minutes
问题描述
我有一个向量Time.Training";格式为小时:分钟:秒(例如
I have a vector "Time.Training" in the format hours:minutes:seconds (e.g.
Time.Training <- c("1:00:00", "0:45:00", "0:30:00", "1:30:00")
我想将其转换为以下格式的分钟:
I would like to convert this into minutes in the format:
Time.Training.Minutes <- c(60, 45, 30, 90)
我想知道是否有人有在 R 中执行此操作的简单方法.
I'm wondering if someone has a straightforward method of doing this in R.
非常感谢.
马特
推荐答案
这里有一些替代方案:
1) chron 包有一个 "times"
类,其中 1 个单位是一天,一天有 60 * 24 分钟,所以:
1) The chron package has a "times"
class in which 1 unit is a day and there are 60 * 24 minutes in a day so:
library(chron)
60 * 24 * as.numeric(times(Time.Training))
给予:
[1] 60 45 30 90
1a) 另一种使用 chron 的方法如下(给出相同的答案):
1a) Another approach using chron is the following (giving the same answer):
library(chron)
ch <- times(Time.training)
60 * hours(ch) + minutes(ch)
2) 这是一种使用 read.table
和矩阵/向量乘法的方法.不需要任何包:
2) Here is an approach using read.table
and matrix/vector multiplication. No packages are needed:
c(as.matrix(read.table(text = Time.Training, sep = ":")) %*% c(60, 1, 1/60))
(使用 "POSIXlt"
可能是没有包的最直接的方法,但另一个答案已经提供了.)
(Using "POSIXlt"
is probably the most straight-forward approach without packages but another answer already provides that.)
这篇关于将小时:分钟:秒转换为分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!