提问人:tovare 提问时间:9/7/2009 最后编辑:Joshua Ulrichtovare 更新时间:10/21/2013 访问量:6604
在 R 中处理 5 分 30 秒等时间段
Dealing with time-periods such as 5 minutes and 30 seconds in R
问:
有没有一种好的方法可以处理 R 中的 05:30(5 分 30 秒)等时间段?
或者,只需几秒钟即可将其转换为整数的最快方法是什么?
我只能转换为日期,无法真正找到时间的数据类型。
我正在将 R 与 zoo 一起使用。
多谢!
几秒钟是解决这个问题的最好方法。我根据我的目的调整了下面的 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)
}
答:
8赞
Dirk is no longer here
9/7/2009
#1
是的,虽然没有“时间”类型,但您可以使用偏移时间:
R> now <- Sys.time()
R> now
[1] "2009-09-07 08:40:32 CDT"
R> class(now)
[1] "POSIXt" "POSIXct"
R> later <- now + 5*60
R> later
[1] "2009-09-07 08:45:32 CDT"
R> class(later)
[1] "POSIXt" "POSIXct"
R> tdelta <- difftime(later, now)
R> tdelta
Time difference of 5 mins
R> class(tdelta)
[1] "difftime"
R>
使用 zoo 包时,您将使用标准类型作为时间索引。zoo 和较新的、强烈推荐的 xts 包都可以使用 ,尤其是紧凑型,用于索引。POSIXt
POSIXt
POSIXct
xts 软件包具有更多的索引功能,Jeff 最近根据 ISO8601-2004(e) 规范添加了对间隔的解析,并提供了这些ISO8601参考和广泛使用的日期和时间格式标准的常见问题解答。要使用此 xts 版本,您可能需要在 r-forge 上切换 xts 开发快照
[编辑:]另外,关于“转换”的问题:一旦你有了对象,这就很容易了
类 POSIXt / POSIXct 将 POSIXct 转换为自纪元以来的(小数)秒。R 比 POSIX 标准更进一步,在这里使用了双精度,因此您可以获得毫秒精度:as.numeric()
R> options("digits.secs"=6) ## needed so that fractional seconds are printed
R> now <- Sys.time(); difftime(Sys.time(), now)
Time difference of 0.000149 secs
和
R> print(as.numeric(now), digits=15) ## print with digits for sub-second time
[1] 1252374404.22975
R>
5赞
Shane
9/8/2009
#2
正如 Dirk 所指出的,有一个叫做“difftime”的对象,但它不能被加/减。
> as.difftime(5, units="mins")
Time difference of 5 mins
> d <- seq(from=as.POSIXct("2003-01-01"), to=as.POSIXct("2003-01-04"), by="days")
> d
[1] "2003-01-01 GMT" "2003-01-02 GMT" "2003-01-03 GMT" "2003-01-04 GMT"
> d + as.difftime(5, units="mins")
[1] "2003-01-01 00:00:05 GMT" "2003-01-02 00:00:05 GMT"
[3] "2003-01-03 00:00:05 GMT" "2003-01-04 00:00:05 GMT"
Warning message:
Incompatible methods ("+.POSIXt", "Ops.difftime") for "+"
看来你现在可以这样做了:
> as.difftime(5, units='mins')
Time difference of 5 mins
> d <- seq(from=as.POSIXct("2003-01-01"), to=as.POSIXct("2003-01-04"), by="days")
> d
[1] "2003-01-01 GMT" "2003-01-02 GMT" "2003-01-03 GMT" "2003-01-04 GMT"
> d + as.difftime(5, unit='mins')
[1] "2003-01-01 00:05:00 GMT" "2003-01-02 00:05:00 GMT"
[3] "2003-01-03 00:05:00 GMT" "2003-01-04 00:05:00 GMT"
> d + as.difftime(5, unit='secs')
[1] "2003-01-01 00:00:05 GMT" "2003-01-02 00:00:05 GMT"
[3] "2003-01-03 00:00:05 GMT" "2003-01-04 00:00:05 GMT"
>
这是最近发布的 R 2.15.0
评论
2赞
Harlan
1/31/2010
是的,difftime 的弱点是标准 R 库的一个非常可悲的方面。
评论