处理时间段,例如R中的5分30秒

处理时间段,例如R中的5分30秒,r,posix,zoo,R,Posix,Zoo,有没有一个很好的方法来处理时间段,例如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 report

有没有一个很好的方法来处理时间段,例如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)
}
#时间-dd hh:mm:ss格式的时间
#(这是阿尔卡特CCS报告cvs导出中使用的格式)
#

time.to.seconds是,虽然没有“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> 


正如德克指出的,有一个叫做“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=“min”)
时差为5分钟
>d
[1] “2003-01-01 GMT”“2003-01-02 GMT”“2003-01-03 GMT”“2003-01-04 GMT”
>d+as.difftime(5,units=“min”)
[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”
警告信息:
“+”的不兼容方法(“+.POSIXt”、“Ops.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, 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"
   >
>as.difftime(5,单位为分钟)
时差为5分钟
>d
[1] “2003-01-01 GMT”“2003-01-02 GMT”“2003-01-03 GMT”“2003-01-04 GMT”
>d+as.difftime(5,单位为分钟)
[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,单位为秒)
[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”
>

这是最近发布的R2.15.0

是的,difftime的弱点是标准R库的一个非常可悲的方面。
> 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"
   >