Good evening everyone. I'm having some trouble creating an O/D based on my DF. My DF has the number of the trip, the index of each stop, the time od departure from each stop and the stops name:
Trip Index Time OD
16 1 a A
16 10 b B
16 20 c C
32 1 d B
32 9 e A
32 13 f C
32 24 g D
I need to have and O/D table, where I have the Origin's stop name | Destination's stop name | and Time of departure (Time in the DF) for each connection:
O D Time
A B a
A C a
B C b
B A d
B C d
B D d
A C e
A D e
C D f
Taking the first trip as an example. It starts in stop "A" and ends inn stop "C" going through stop "B". So, for passengers in "A", they start their journey to "B" at time "a" and their journey to "C" at same time "a". For passengers in "B", they start their journey to "C" at time "b". From "C" you can't go nowhere with this trip. Then we must see the next trip. And so on.
I've started to try with "for" loops and if else inside the for to start comparing the first line of the first trip with the second and third, then the second line of the first trip with the third, than passing to the next trip since there is no 4th leg. At least this is my rationale, don't know if it is clear or if it even makes sense.
Thank you!
Try this, with one simple helper-function:
library(dplyr)
odfunc <- function(tm, od) {
mtx <- t(combn(length(od), 2))
tibble::tibble(O = od[mtx[,1]], D = od[mtx[,2]], Time = tm[mtx[,1]])
}
df %>%
group_by(Trip) %>%
do(with(., odfunc(Time, OD))) %>%
ungroup()
# # A tibble: 9 x 4
# Trip O D Time
# <int> <chr> <chr> <chr>
# 1 16 A B a
# 2 16 A C a
# 3 16 B C b
# 4 32 B A d
# 5 32 B C d
# 6 32 B D d
# 7 32 A C e
# 8 32 A D e
# 9 32 C D f
Data:
df <- read.table(header=TRUE, text="
Trip Index Time OD
16 1 a A
16 10 b B
16 20 c C
32 1 d B
32 9 e A
32 13 f C
32 24 g D")