I would like to calculate the difference between times in R for each observation given that it concerns bus arrivals and timetables. The code i have so far is:
for (i in ida1d$DATA_TRAMA) {
for (j in horidat$CORD4) {
if((ida1d$DATA_TRAMA - horidat$CORD4 < ida1d$diff)) {
ida1d$diff <- ida1d$DATA_TRAMA - horidat$CORD4
}
}
}
I have these dataframes:
ida1d
which has the specific information I wanthoridat
which has the timetables- both
ida1d$DATA_TRAMA
andhoridat$CORD4
are in POSIXct%Y-m%-%d %H:%M:%S
format.
Now what I was trying to do is to have a column in the ida1d table with the differences. The problem is that for the first few observations it calculates the differences correctly, but from a certain point on it doesn't, the number of observations for horidat
(timetable) is 75 and for the ida1d
is 88 I think the problem with the calculations could be because of this, thus trying to approach the problem with for loops, but I think i'm missing something...
The date and time for ida1d is the time that the bus arrived at the bus stop throughout the day :
ida1d$DATA_TRAMA
[1] 2010-10-01 00:00:08
2010-10-01 00:29:45
2010-10-01 06:22:56
2010-10-01 06:38:55
2010-10-01 06:52:41
2010-10-01 07:05:08
2010-10-01 07:15:17
2010-10-01 07:25:14
2010-10-01 07:38:25
2010-10-01 07:44:55
2010-10-01 07:54:44
2010-10-01 08:05:05
2010-10-01 08:14:43
2010-10-01 08:24:11
2010-10-01 08:33:29
2010-10-01 08:46:26
2010-10-01 08:54:40
2010-10-01 09:04:34
2010-10-01 09:14:53
And this is the timetable for the bus (horidat
)
horidat$CORD4
[2] 2010-10-01 00:00:00
2010-10-01 00:30:00
2010-10-01 06:25:00
2010-10-01 06:45:00
2010-10-01 07:00:00
2010-10-01 07:15:00
2010-10-01 07:30:00
2010-10-01 07:45:00
2010-10-01 07:57:00
2010-10-01 08:09:00
2010-10-01 08:21:00
2010-10-01 08:32:00
2010-10-01 08:43:00
2010-10-01 08:54:00
2010-10-01 09:06:00
2010-10-01 09:18:00
And i can calculate the difference between [1] and [2] as we can see below the results [3]:
ida1d$diff
Time differences in secs
[3] 8
-15
-124
-365
-439
-592
-883
-1186
-1115
-1445
-1576
-1615
-1697
-1789
-1951
-1894
-2120
-2246
As we can see it starts off well in beginning and then it gets wrong the rest of the calculations and that is my problem, and i wanted to get the smallest value of the difference to be in the column for each observation, perhaps it uses other values and thus the wrong calculations i guess...