gpt4 book ai didi

r - 映射距离 : controlling for traffic

转载 作者:行者123 更新时间:2023-12-04 11:34:32 28 4
gpt4 key购买 nike

我正在做一个项目,我需要获得从“上车”到“下车”坐标的最短距离和时间。在我的数据集中,我有一个变量指示“trip_distance”和“pickup_date”,我的任务是计算“trip_distance”变量偏离谷歌估计距离的程度,并通过控制出发时间来计算每次旅行所需的时间时间。

这是我的一小部分数据样本(大约有 150 万行,我正试图找到一种方法来限制 <2,500 次查询)

trip_distance   pickup_datetime     pickup                  dropoff
1 8.1 2011-01-01 23:13:56 40.77419%2C-73.872608 40.78055%2C-73.955042
2 10.6 2011-01-04 17:12:49 40.7737%2C-73.870721 40.757007%2C-73.971953
3 15.9 2011-01-05 18:41:53 40.773761%2C-73.87086 40.707277%2C-74.007301

代码:

library(ggmap)
rownames(X) <- NULL
res <- mapdist(from= X$pickup,
to = X$dropoff,
mode = "driving" ,
output = "simple", messaging = FALSE, sensor = FALSE,
language = "en-EN", override_limit = FALSE, departure_time= X$pickup_date)

我得到的错误是:

 Error in mapdist(from = X$pickup, to = X$dropoff, mode = "driving",      output = "simple",  :  unused argument (departure_time = X$pickup_date)

有什么方法可以使用 mapdist 控制流量吗?

输出(头(X))

structure(list(pickup_datetime = structure(c(1293923636, 1294161169, 
1294252913, 1294259376, 1294419723, 1293903309), class = c("POSIXct",
"POSIXt"), tzone = ""), trip_distance = c(8.1, 10.6, 15.9, 8.9,
11.5, 9.6), pickup = c("40.77419,-73.872608", "40.7737,-73.870721",
"40.773761,-73.87086", "40.773776,-73.870908", "40.774161,-73.87302",
"40.774135,-73.8749"), dropoff = c("40.78055,-73.955042", "40.757007,-73.971953",
"40.707277,-74.007301", "40.770568,-73.95468", "40.758284,-73.986621",
"40.758691,-73.961359")), .Names = c("pickup_datetime", "trip_distance",
"pickup", "dropoff"), row.names = c(NA, 6L), class = "data.frame")

最佳答案

我写了包 googleway访问谷歌地图 API,您可以在其中指定您的 API key ,从而使用 API 提供的功能(例如出发时间和路况)

然而,为此你需要使用开发版本,因为我注意到 traffic_model 中的一个小错误。这将在下一个版本中修复。

devtools::install_github("SymbolixAU/googleway")
library(googleway)

key <- "your_api_key"

## data.frame of origin & destination coordiantes
## you can obviously add in a 'pickup' datetime column too,
## but remembering that for Google API it must be in the future
df <- data.frame(orig_lat = c(40.77419, 40.7737, 40.773761),
orig_lon = c(-73.872608, -73.870721, -73.87086),
dest_lat = c(40.78055, 40.757007, 70.707277),
dest_lon = c(-73.955042, -73.971953,-74.007301))

现在您可以使用您首选的循环方法获取 data.frame 中每一行的每组点之间的距离

例如

lst <- apply(df, 1, function(x) { 
google_distance(origins = list(c(x["orig_lat"], x["orig_lon"])),
destinations = list(c(x["dest_lat"], x["dest_lon"])),
departure_time = Sys.time() + (24 * 60 * 60),
traffic_model = "best_guess",
key = key)
})

然后你可以访问返回列表中的数据

lst[[1]]$origin_addresses
# [1] "Central Terminal Dr, East Elmhurst, NY 11371, USA"
lst[[1]]$destination_addresses
# [1] "1294-1296 Lexington Ave, New York, NY 10128, USA"
lst[[1]]$rows$elements
# [[1]]
# distance.text distance.value duration.text duration.value duration_in_traffic.text duration_in_traffic.value status
# 1 12.8 km 12805 21 mins 1278 23 mins 1355 OK

关于r - 映射距离 : controlling for traffic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37167580/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com