We can use melt
from data.table
which can take multiple measure
columns.
library(data.table)
dM <- melt(setDT(test), measure=patterns('lon', 'lat'),
value.name=c('longitude', 'latitude'))
#change the 'variable' column from numeric index to 'from/to'
dM[, variable:= c('from', 'to')[variable]]
#create a sequence column grouped by 'variable'
dM[,i1:= 1:.N ,variable]
#order based on the 'i1'
res <- dM[order(i1)][,i1:=NULL]
res
# dis dur method variable longitude latitude
#1: 10 30 car from -1.9800 55.3009
#2: 10 30 car to -1.4565 76.8888
#3: 20 40 car from -1.5678 55.3416
#4: 20 40 car to -1.3424 65.8999
#5: 30 60 Bicycle from -1.3240 55.1123
#6: 30 60 Bicycle to -1.4566 76.9088
#7: 40 90 Bicycle from -1.4560 55.2234
#8: 40 90 Bicycle to -1.1111 25.3344