I have data.tableone that gives me connections between locations ( originand destination) for different bus routes ( route_id).
library(data.table)
library(magrittr)
dt <- data.table( origin = c('A','B','C', 'F', 'G', 'H'),
destination = c('B','C','D', 'G', 'H', 'I'),
freq = c(2,2,2,10,10,10),
route_id = c(1,1,1,2,2,2), stringsAsFactors=FALSE )
For the purposes of what I would like to do, if there is route_idone that gives the connection A-Band the connection B-C, then I want to add a connection to the data A-Cfor the same route_id, etc.
The problems . So far I have created simple code that does this work, but:
- it uses
for loop, which takes a lot of time (my real data contain hundreds of thousands of observations). - . . ,
B-C, C-B.

for (i in unique(dt$route_id)) {
temp <- dt[ route_id== i,]
subset_of_pairs <- expand.grid(temp$origin, temp$destination) %>% setDT()
setnames(subset_of_pairs, c("origin", "destination"))
dt <- rbind(dt, subset_of_pairs, fill=T)
}
dt[, route_id := route_id[1L], by=origin]
dt[, freq := freq[1L], by=route_id]
dt[, origin := as.character(origin) ][, destination := as.character(destination) ]
dt <- dt[ origin != destination, ][order(route_id, origin, destination)]
dt <- unique(dt)
origin destination freq route_id
1: A B 2 1
2: A C 2 1
3: A D 2 1
4: B C 2 1
5: B D 2 1
6: C D 2 1
7: F G 10 2
8: F H 10 2
9: F I 10 2
10: G H 10 2
11: G I 10 2
12: H I 10 2