【发布时间】:2010-11-26 05:48:11
【问题描述】:
在R中是否有处理时间段的好方法,例如05:30(5分30秒)?
或者,用几秒钟将其转换为整数的最快方法是什么?
我只能转换为日期,无法真正找到时间的数据类型。
我正在使用 R 和动物园。
非常感谢!
秒是处理这个问题的最佳方式。我根据我的目的调整了 Shane 的以下代码,结果如下。
# time - time in the format of dd hh:mm:ss
# (That's the format used in cvs export from Alcatel CCS reports)
#
time.to.seconds <- function(time) {
t <- strsplit(as.character(time), " |:")[[1]]
seconds <- NaN
if (length(t) == 1 )
seconds <- as.numeric(t[1])
else if (length(t) == 2)
seconds <- as.numeric(t[1]) * 60 + as.numeric(t[2])
else if (length(t) == 3)
seconds <- (as.numeric(t[1]) * 60 * 60
+ as.numeric(t[2]) * 60 + as.numeric(t[3]))
else if (length(t) == 4)
seconds <- (as.numeric(t[1]) * 24 * 60 * 60 +
as.numeric(t[2]) * 60 * 60 + as.numeric(t[3]) * 60 +
as.numeric(t[4]))
return(seconds)
}
【问题讨论】: