Suppose we have a data frame of a delivery agency where rows represent deliveries as follows:
Agent Time of Delivery Address
Alpha 12-30-2012 08:12 Location 1
Alpha 12-30-2012 08:18 Location 2
Alpha 12-30-2012 10:12 Location 3
Alpha 12-30-2012 10:25 Location 4
Beta 12-30-2012 08:30 Location 2
Beta 12-30-2012 09:44 Location 5
Beta 12-30-2012 18:11 Location 1
Gamma 12-30-2012 07:05 Location 6
Gamma 12-30-2012 08:30 Location 4
Gamma 12-30-2012 08:33 Location 3
Gamma 12-30-2012 14:12 Location 1
Gamma 12-30-2012 22:05 Location 2
Given the dataset above, I'd like to calcute the length of daily routes by each delivery agent in km (assuming that there is a function that can calculate the distance of two addresses - is there?). My problem is twofold:
- I must follow the time sequence in order to follow the real physical route of the agent, and "substract" the former location "from" the latter, but how could one compare the actual row with the "previous" row in R?
- Knowing that the agents depart every moring from and arrive every evening to the company HQ, I must add the HQ-first_address_of_the_day_of_each_agent and last_address_of_the_day_of_each_agent-HQ to the daily calcuation by agents, which also implies that I can figure out which are the "neighbouring" rows (again a supposed comparison with the previous/next timestamp).
But how?