2016-07-27 80 views
-1

我有兩列我想以這種方式循環它應該計算(i)的到達 - (i + 1)的出發,並且值應該存儲在一個新列中。R編程循環差異

departure       arrival 
2012-01-26 08:15:00     2012-01-26 08:50:00 
2012-01-26 11:30:00    2012-01-27 16:00:00 
2012-01-27 18:00:00    2012-01-27 23:05:00 
2012-02-11 00:20:00     2012-02-11 09:10:00 

expected output 
waiting time 
(2012-01-26 08:50:00)-(2012-01-26 11:30:00) = 2 hr 40 min 
(2012-01-27 16:00:00)-(2012-01-27 18:00:00) = 2 hrs 
(2012-01-27 23:05:00)- (2012-02-11 00:20:00)=14 days 13 hrs, 349 hrs 

請幫忙完成上述數據的代碼。在RI嘗試,但也存在一些誤區

for (i :length(data frame){ 
i <- i[17] #index of arrival column 
j <- i+1[18] #index of departure column 
diff = i-j 
return diff 
} 
diff = waiting time 

有在計算到達和離開的區別

我已經轉換由POSXClt

回答

0

這將使用包中的數據出發和到達的一些錯誤lubridate

d$departure <- lubridate::ymd_hms(d$departure) 
d$arrival <- lubridate::ymd_hms(d$arrival) 

d$difference <- d$departure - d$arrival 
d 
  departure    arrival difference 
1 2012-01-26 08:15:00 2012-01-26 08:50:00 -35 mins 
2 2012-01-26 11:30:00 2012-01-27 16:00:00 -1710 mins 
3 2012-01-27 18:00:00 2012-01-27 23:05:00 -305 mins 
4 2012-02-11 00:20:00 2012-02-11 09:10:00 -530 mins 
+0

謝謝,我需要抵達ist值和離開第二個值之間的區別即(2012-01-26 08:50:00) - (2012-01-26 11:30:00)小時40分鐘 –