I have a dataset that summarises car trips, but it does not identify how many unique cars there are. I would like to create a loop/if statement that assigns a unique number based on a location and time a trip starts to figure out an approximate unique number of cars.
So for example, if dropoff location of the first car matches pickup location of the second car, and time frame is within 2 minutes, assign the same car number as the first car. If completely different assign a new number.
I tried different options but can't make it work(a beginner). Any help at this time is greatly appreciated. (R or Python)
This is roughly what I have:
| Pickup time | Dropoff time | Pickup location | Dropoff location |
|---|---|---|---|
| 2016-06-09 21:06:36 | 2016-06-09 21:13:08 | A | B |
| 2016-06-09 21:13:31 | 2016-06-09 21:23:59 | A | C |
| 2016-06-09 21:13:45 | 2016-06-09 21:26:29 | B | C |
| 2016-06-09 21:15:33 | 2016-06-09 21:44:31 | A | B |
| 2016-06-09 21:24:49 | 2016-06-09 21:39:29 | C | D |
This is what I would like to achieve:
| Pickup time | Dropoff time | Pickup location | Dropoff location | Car # |
|---|---|---|---|---|
| 2016-06-09 21:06:36 | 2016-06-09 21:13:08 | A | B | 1 |
| 2016-06-09 21:13:31 | 2016-06-09 21:23:59 | A | C | 2 |
| 2016-06-09 21:13:45 | 2016-06-09 21:24:29 | B | C | 1 |
| 2016-06-09 21:15:33 | 2016-06-09 21:44:31 | A | B | 3 |
| 2016-06-09 21:24:49 | 2016-06-09 21:39:29 | C | D | 2 |