Here is an alternative approach using package tidyr
(a popular package for data munging), which avoids for
loop.
library(tidyr)
test <- data.frame(dis = c(10,20,30,40),dur=c(30,40,60,90),method=c("car","car","Bicycle","Bicycle"),to_lon=c(-1.980,-1.5678,-1.324,-1.456),to_lat=c(55.3009,55.3416,55.1123,55.2234),from_lon=c(-1.4565,-1.3424,-1.4566,-1.1111),from_lat=c(76.8888,65.8999,76.9088,25.3344))
test$id <- 1:dim(test)[1]
# gather latitude columns
d1 <- gather(data = test,
key = direction,
value = latitude,
to_lat, from_lat)
# gather longitude columns
d2 <- gather(data = test,
key = direction,
value = longitude,
to_lon, from_lon)
d3 <- cbind(d1[,c("direction","dis","dur","method","latitude")],d2[,c("longitude","id"),drop=FALSE])
# Create names
dir <- unlist(strsplit(d3$direction,"_"))
dir <- dir[seq(from = 1, to = length(dir), by = 2)]
# Factor and sort
d3$direction <- factor(dir)
d3[order(d3$id),]