我希望这个问题还没有解决,但我找不到任何答案。
下面的 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" ...