在 R 中处理 5 分 30 秒等时间段

发布于 2024-08-04 00:13:45 字数 931 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风向决定发型 2024-08-11 00:13:45

是的,虽然没有“时间”类型,但您可以使用偏移时间:

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> 

当您使用 < 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,因此您可以获得毫秒精度:

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> 

Yes, while there is no 'time' type, you can use an offset time:

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> 

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 use POSIXt, and especially the compact POSIXct 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:

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

and

R> print(as.numeric(now), digits=15)   ## print with digits for sub-second time
[1] 1252374404.22975
R> 
原野 2024-08-11 00:13:45

正如 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

As Dirk points out, there is an object called "difftime", but it can't be added/subtracted.

> 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 "+" 

Seems you can now do this:

  > 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"
   >

This is with the recently released R 2.15.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文