多条路线的行驶距离
Driving Distance with Multiple Routes
我正在尝试使用带有 route()
函数的 ggmap 包从由多对坐标组成的 tibble 中获取行驶距离和时间。
数据:
locations <- tibble(start_loc = c("44.025135, -69.892204", "44.0175617, -70.0108097"), end_loc = "43.9195136, -69.9665309")
我想要结束的是 locations
tibble 附加两列,total travel time 和 total距离。为此,我可能需要遍历 locations
中的每一行,将每对坐标传递给 route()
函数。问题是 route()
的输出为路线中的每个步骤生成一行。
我目前拥有的:
library(ggmap)
library(dplyr)
locations
# A tibble: 2 x 2
# start_loc end_loc
# <chr> <chr>
#1 44.025135, -69.892204 43.9195136, -69.9665309
#2 44.0175617, -70.0108097 43.9195136, -69.9665309
for(i in 1:nrow(locations)) {
route_output <- route(from = locations$start_loc[i], to = locations$end_loc[i],
mode = c("driving"), output = c("simple"), units ="mi", structure = "legs")
}
route_output
# A tibble: 6 x 11
# m km miles seconds minutes hours start_lon start_lat end_lon end_lat route
# <int> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
#1 2608 2.61 1.62 184 3.07 0.0511 -70.0 44.0 -70.0 44.0 A
#2 524 0.524 0.326 32 0.533 0.00889 -70.0 44.0 -70.0 44.0 A
#3 3106 3.11 1.93 232 3.87 0.0644 -70.0 44.0 -70.1 44.0 A
#4 9516 9.52 5.91 528 8.8 0.147 -70.1 44.0 -70.0 43.9 A
#5 88 0.088 0.0547 9 0.15 0.0025 -70.0 43.9 -70.0 43.9 A
#6 1960 1.96 1.22 166 2.77 0.0461 -70.0 43.9 -70.0 43.9 A
这里显示的是最后一对 start_loc
end_loc
路线的每一步。所以它没有像我想要的那样循环,我也没有得到摘要信息。我什至没有办法将 route_output
tibble 重新添加到 `locations tibble。总之,我真的卡死了
关于做什么的建议?如果 ggmap 包不适合这个,我还应该使用什么?
试试这样的东西:
library(ggmap)
locations <- data.frame(start_loc = c("44.025135, -69.892204", "44.0175617, -70.0108097"), end_loc = "43.9195136, -69.9665309")
trip_dist_time <- function(from, to, mode) {
results <- ggmap::route(from, to, mode)
data.frame(time_minutes = sum(results$minutes),
distance_km = sum(results$km),
mode = mode)
}
# Select a travel mode from "driving", "walking", "bicycling", "transit"
travel_mode <- "driving"
results <- mapply(trip_dist_time, locations$start_loc, locations$end_loc, travel_mode, SIMPLIFY = FALSE)
results <- Reduce(rbind, results)
results <- cbind(locations, results)
results
您将获得:
start_loc end_loc time_minutes distance_km mode
1 44.025135, -69.892204 43.9195136, -69.9665309 15.95 16.476 driving
2 44.0175617, -70.0108097 43.9195136, -69.9665309 19.40 17.802 driving
我正在尝试使用带有 route()
函数的 ggmap 包从由多对坐标组成的 tibble 中获取行驶距离和时间。
数据:
locations <- tibble(start_loc = c("44.025135, -69.892204", "44.0175617, -70.0108097"), end_loc = "43.9195136, -69.9665309")
我想要结束的是 locations
tibble 附加两列,total travel time 和 total距离。为此,我可能需要遍历 locations
中的每一行,将每对坐标传递给 route()
函数。问题是 route()
的输出为路线中的每个步骤生成一行。
我目前拥有的:
library(ggmap)
library(dplyr)
locations
# A tibble: 2 x 2
# start_loc end_loc
# <chr> <chr>
#1 44.025135, -69.892204 43.9195136, -69.9665309
#2 44.0175617, -70.0108097 43.9195136, -69.9665309
for(i in 1:nrow(locations)) {
route_output <- route(from = locations$start_loc[i], to = locations$end_loc[i],
mode = c("driving"), output = c("simple"), units ="mi", structure = "legs")
}
route_output
# A tibble: 6 x 11
# m km miles seconds minutes hours start_lon start_lat end_lon end_lat route
# <int> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
#1 2608 2.61 1.62 184 3.07 0.0511 -70.0 44.0 -70.0 44.0 A
#2 524 0.524 0.326 32 0.533 0.00889 -70.0 44.0 -70.0 44.0 A
#3 3106 3.11 1.93 232 3.87 0.0644 -70.0 44.0 -70.1 44.0 A
#4 9516 9.52 5.91 528 8.8 0.147 -70.1 44.0 -70.0 43.9 A
#5 88 0.088 0.0547 9 0.15 0.0025 -70.0 43.9 -70.0 43.9 A
#6 1960 1.96 1.22 166 2.77 0.0461 -70.0 43.9 -70.0 43.9 A
这里显示的是最后一对 start_loc
end_loc
路线的每一步。所以它没有像我想要的那样循环,我也没有得到摘要信息。我什至没有办法将 route_output
tibble 重新添加到 `locations tibble。总之,我真的卡死了
关于做什么的建议?如果 ggmap 包不适合这个,我还应该使用什么?
试试这样的东西:
library(ggmap)
locations <- data.frame(start_loc = c("44.025135, -69.892204", "44.0175617, -70.0108097"), end_loc = "43.9195136, -69.9665309")
trip_dist_time <- function(from, to, mode) {
results <- ggmap::route(from, to, mode)
data.frame(time_minutes = sum(results$minutes),
distance_km = sum(results$km),
mode = mode)
}
# Select a travel mode from "driving", "walking", "bicycling", "transit"
travel_mode <- "driving"
results <- mapply(trip_dist_time, locations$start_loc, locations$end_loc, travel_mode, SIMPLIFY = FALSE)
results <- Reduce(rbind, results)
results <- cbind(locations, results)
results
您将获得:
start_loc end_loc time_minutes distance_km mode
1 44.025135, -69.892204 43.9195136, -69.9665309 15.95 16.476 driving
2 44.0175617, -70.0108097 43.9195136, -69.9665309 19.40 17.802 driving