1

我希望这个问题还没有解决,但我找不到任何答案。

下面的 R 会给我一个“6.783333 小时的时差”的输出,这正是我想要看到的。

Start = "2013-04-19 16:42"
End =  "2013-04-19 23:29"


dtm <- strptime(c(Start), format = "%Y-%m-%d %H:%M", tz = "CET")
dtms <- strptime(c(End), format = "%Y-%m-%d %H:%M", tz = "CET")


t_time = dtms - dtm

t_time

现在在我下面的循环中,它不会将文本写入我的列,而只会将数值写入。

例如,我不知道数字 6.78333 是代表小时、秒、天、月还是年。

想看专栏写“时差6.783333小时”

x <-1:555500
for(i in seq(along=x))

{

LookUpAccount = DateCorrection$Account[i]
LoopData =  DateCorrection[ DateCorrection$Account ==LookUpAccount , ]
FrameLoop = as.data.frame(LoopData$Account)

    if(i > 1){
    if(DateCorrection$Account[i] == DateCorrection$Account[i-1] )
    {
    s = DateCorrection$Account[i]

    DateCorrection$AcctCheck[i] = s
    dtm <- strptime(c(DateCorrection$NewDate[i]), format = "%Y-%m-%d %H:%M", tz = "CET")
    dtms <- strptime(c(DateCorrection$NewDate[i-1] ), format = "%Y-%m-%d %H:%M", tz = "CET")


    t_time = dtm - dtms

    #R is trimming off the time reference description and only leaving me a numeric number
    DateCorrection$TimeDiff[i] = t_time

    }
    }
}

附上数据框

    'data.frame':   555500 obs. of  18 variables:
     $ Account    : chr  "5345234652" "5345234652" "5345234652" "5345234652" ...
     $ Date       : Date, format: "2013-04-18" "2013-04-18" "2013-04-18" "2013-04-18" ...
     $ ContactType: chr  "GH" "FF" "GH" "GH" ...
     $ ContactCode: chr  "226" "1309" "1309" "1306" ...
     $ EmpID      : chr  "5722  " "1883  " "9588  " "C45061  " ...
     $ Duration   : chr  "393" "2589" "690" "184" ...
     $ OBS        : chr  "19013359390" "19013360497" "19013361117" "19013361956" ...
     $ Month      : chr  "Apr" "Apr" "Apr" "Apr" ...
     $ Year       : chr  "2013" "2013" "2013" "2013" ...
     $ Freq       : int  28 28 28 28 28 28 28 28 28 28 ...
     $ Mil        : chr  "PM" "PM" "PM" "PM" ...
     $ Hour       : num  14 15 16 17 22 14 15 15 16 16 ...
     $ Min        : num  45 45 22 25 1 17 38 43 10 42 ...
     $ HourMin    : chr  "1445" "1545" "1622" "1725" ...
     $ NewDate    : chr  "2013-04-18 14:45" "2013-04-18 15:45" "2013-04-18 16:22" "2013-04-18 17:25" ...
     $ TimeDiff   : chr  "XXXXXXXXXXXXXXXXXXXXXXXXXXXX" "1" "37" "1.05" ...
     $ SecondsDiff: num  0 0 0 0 0 0 0 0 0 0 ...
     $ AcctCheck  : chr  "Y" "5345234652" "3423253453" "34787644763" ...
4

3 回答 3

1
x <- capture.output(print.difftime(dtms-dtm))
#[1] "Time difference of 6.783333 hours"

最好指定单位(而不是自动选择它)并只存储数值:

difftime(dtms, dtm, units = "hours")
#Time difference of 6.783333 hours
difftime(dtms, dtm, units = "days")
#Time difference of 0.2826389 days
于 2013-08-05T14:05:44.847 回答
1

值的显示模式由值的类控制。

class(t_time)difftime;_ 因此显示这样的值调用函数print.difftimeclass(DateCorrection$TimeDiff),另一方面,是numeric。但是,您可以简单地重新分配列的类:

class(DateCorrection$TimeDiff) <- 'difftime'

但是,请注意,如果您显示数据框,时差仍将以缩写格式显示。要获得长格式,只需打印该TimeDiff列。

于 2013-08-05T14:09:24.083 回答
0

您可以使用as.difftime.

as.difftime(tim, format = "%X", units = "auto")

这里和例子:

(dd <- data.frame(TimeDiff = 1))
  TimeDiff
1        1
## you coerce it now 
dd$TimeDiff <- as.difftime(dd$TimeDiff,units='hours')
dd
  TimeDiff
1  1 hours
于 2013-08-05T15:03:09.683 回答