R:转换小时:分:秒 [英] R: Convert hours:minutes:seconds
问题描述
我有一个向量Time.Training,格式为hours:minutes:seconds(例如
Time.Training< - c(1:00:00,0:45:00,0:30:00,1:30:00)
我想将其转换成几分钟格式:
Time.Training.Minutes< -c(60,45,30,90)
我想知道如果有人在R中有一个直接的方法。
非常感谢。
Matt
这里有一些选择:
1) chron软件包有一个times
类,其中1个单位是一天,每天有60 * 24分钟,所以:
库(chron)
/ pre>
60 * 24 * as.numeric(times(Time.Training))
给予:
[1] 60 45 30 90
1a)使用chron的另一种方法是f以下(给出相同的答案):
库(chron)
ch< - times Time.training)
60 *小时(ch)+分钟(ch)
2)这是使用
read.table
和矩阵/向量乘法的方法。不需要软件包:c(as.matrix(read.table(text = Time.Training,sep =: ))%*%c(60,1,1/60))
(使用
POSIXlt
可能是没有包的最直接的方法,但另一个答案已经提供了。)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)
I'm wondering if someone has a straightforward method of doing this in R.
Many thanks.
Matt
解决方案Here are some alternatives:
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))
giving:
[1] 60 45 30 90
1a) Another approach using chron is the following (giving the same answer):
library(chron) ch <- times(Time.training) 60 * hours(ch) + minutes(ch)
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))
(Using
"POSIXlt"
is probably the most straight-forward approach without packages but another answer already provides that.)这篇关于R:转换小时:分:秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!