I have a massive data.frame with the starting and ending (latitudes & longtitude) and am using the georoute
function from the taRifx.geo
package, to find out how far and how much time does it take to drive from A to B.
the data looked something like this (both latlon
and latlon_end
are class of characters
:
> LL[1:10,14:15]
latlon latlon_end
1 52.481466 13.317647 52.518811 13.413034
2 52.518811 13.413034 52.504182 13.318051
3 52.504182 13.318051 52.502236 13.305396
4 52.502236 13.305396 52.548096 13.355104
5 52.548096 13.355104 52.569865 13.410967
6 52.569865 13.410967 52.54505 13.419071
7 52.54505 13.419071 52.527736 13.378182
8 52.527736 13.378182 52.495678 13.343019
9 52.495678 13.343019 52.496712 13.341767
10 52.496712 13.341767 52.458631 13.32529
and here is a for
loop that I have written for the purpose:
for(i in 38753:100000){
DT[i,]=tryCatch(t(as.matrix(unlist(georoute( c(as.character(LL$latlon[i]),
as.character(LL$latlon_end[i])),
verbose=TRUE, returntype=c("time", "distance"))),
nrow = 1, ncol = 2)),
error=function(a) {"."} )
}
the base function here, georoute
basically give out a list of two elements, time
and distance
, that's why I have to unlist them first before binding all them into a dataframe. for the trycatch
function, that's to deal with occasional error for the georoute
, I have no idea how alternatively I can do this..
I have really tried a lot of methods but only this seems to have to work out for me, since somehow this georoute
function seems to take only one pair of latlon & latlon_end at one time so I have to do this row by row. However with a few hundred thousands of entries this is taking me days or even weeks to process all this data. I know I should go in the package and understand the codes behind(link inserted) just so I know what is a better fit for this purpose, yet the script is too advanced for my level and I don't even know what in the script that I am looking for to be exact. I guess I could use the lapply function for this but I just can't make it work.
Any help or tips or ideas would be very really super greatly appreciated!
ps. update for original georoute
returns
> georoute(c(as.character(LL$latlon[1]), as.character(LL$latlon_end[1])), verbose = FALSE, returntype = c("time","distance"))
distance time
1 9.03 1338
> georoute(c(as.character(LL$latlon[1:3]), as.character(LL$latlon_end[1:3])), verbose = FALSE, returntype = c("time","distance"))
distance time
1 35.599 5275
> class(georoute(c(as.character(LL$latlon[1]), as.character(LL$latlon_end[1])), verbose = FALSE, returntype = c("time","distance")))
[1] "data.frame"
and I think the distance
and time
returned are numeric because the summary of that shows the 4 quantiles, mean, medians etc.