在 R 中处理 5 分 30 秒等时间段
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)
}
Is there a good way to deal with time periods such as 05:30 (5 minutes, 30 seconds) in R?
Alternatively what's the fastest way to convert it into an integer with just seconds?
I can only convert to dates and can't really find a data type for time.
I'm using R with zoo.
Thanks a lot !
Seconds was the best way to deal with this. I adapted Shane's code below to my purposes, here's the result.
# 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)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,虽然没有“时间”类型,但您可以使用偏移时间:
当您使用 < strong>zoo 包,您正在使用标准
POSIXt
类型作为时间索引。 Zoo 和较新且高度推荐的 xts 包可以使用 POSIXt,尤其是紧凑的 POSIXct 类型进行索引。xts 包具有更多的索引功能,Jeff 最近根据 ISO8601-2004(e) 规范添加了间隔解析,并给出了这些参考
ISO8601 和 广泛使用的日期和时间格式标准的常见问题解答。要使用此 xts 版本,您可能需要切换 r-forge 上的 xts 开发快照< /a>
[编辑:] 另外,关于“转换”的问题:一旦有了对象,这很容易
POSIXt / POSIXct 类的 as.numeric() 会将 POSIXct 转换为自纪元以来的(小数)秒数。 R 比 POSIX 标准更进一步,在这里使用了 double,因此您可以获得毫秒精度:
并且
Yes, while there is no 'time' type, you can use an offset time:
When you use the zoo package, you are using standard
POSIXt
types for your times indices. Both zoo and the newer and also highly-recommended xts package can usePOSIXt
, and especially the compactPOSIXct
type, for indexing.The xts package has a lot more indexing functionality, and Jeff recently added parsing of intervals according to the ISO8601-2004(e) specification and gives these references for
ISO8601 and a FAQ for widely used standars for date and time formats. To use this xts version, you may need to switch the the xts development snapshot on r-forge
[Edit:] Also, regarding the question on 'conversion': this is easy once you have objects
of class POSIXt / POSIXct as
as.numeric()
will convert POSIXct to (fractional) seconds since the epoch. R goes further than the POSIX standard and uses a double here, so you get millisecond precision:and
正如 Dirk 指出的,有一个名为“difftime”的对象,但它不能被添加/减去。
看来你现在可以这样做:
这是最近发布的 R 2.15.0
As Dirk points out, there is an object called "difftime", but it can't be added/subtracted.
Seems you can now do this:
This is with the recently released R 2.15.0