【问题标题】:Dealing with time-periods such as 5 minutes and 30 seconds in R处理 R 中的 5 分 30 秒等时间段
【发布时间】: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)
}

【问题讨论】:

    标签: r posix zoo


    【解决方案1】:

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

    【讨论】:

    • 是的,difftime 的弱点是标准 R 库的一个非常可悲的方面。
    【解决方案2】:

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

    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 包时,您将使用标准的 POSIXt 类型作为时间索引。 zoo 和较新且高度推荐的xts 包都可以使用POSIXt,尤其是紧凑的POSIXct 类型进行索引。

    xts 包具有更多索引功能,Jeff 最近根据 ISO8601-2004(e) 规范添加了间隔解析,并提供了这些参考 ISO8601FAQ for widely used standars for date and time formats。要使用这个xts版本,你可能需要切换xts development snapshot on r-forge

    [编辑:] 另外,关于“转换”的问题:一旦你有了对象,这很容易 POSIXt / POSIXct 类的 as.numeric() 会将 POSIXct 转换为自纪元以来的(小数)秒。 R 比 POSIX 标准更进一步,在这里使用双精度,因此您可以获得毫秒精度:

    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> 
    

    【讨论】:

      猜你喜欢
      • 2011-03-06
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2016-08-22
      相关资源
      最近更新 更多