gpt4 book ai didi

r - ggmap mapdist 函数重复计算某些 O-D 对

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

我想使用 ggmap 中的 mapdist 函数查找行驶距离和时间。该函数适用于以纬度/经度形式给出的大多数 Origin Destination 对。但是,对于某些 O-D,输出是重复的。

在给定的示例中,第 8 对和第 12 对 O-D 发生重复。结果,尽管输入只有 12 行,但输出有 14 行数据。

有什么建议?

O <- c("37.8405983 -122.2544365",
"37.7589152 -122.4062322",
"37.7517621 -122.4062364",
"37.9659228 -122.505765",
"37.6969336 -121.8650777",
"37.9457334 -122.3152847",
"38.1237134 -122.23956",
"37.8911093 -122.2813663",
"37.9067555 -122.0145836",
"38.0673887 -122.2185562",
"37.7807761 -122.2102385",
"37.8911093 -122.2813663")

D <- c("37.8454493 -122.2833803",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625")

DF <- cbind(O, D); DF <- as.data.frame(DF)

GMAPDist <- mapdist(from = O, to = D, mode = "driving",output = "simple")

但是,如果我只为一对 O-D 调用该函数,则输出符合预期。见下文:
mapdist(from = c("37.9067555 -122.0145836"), to = c("37.831217 -122.2719625"), mode = "driving",
+ output = "simple")

最佳答案

这是使用循环的解决方案。请注意,我更改了输入变量的名称。

输入数据

from <- c("37.8405983 -122.2544365",
"37.7589152 -122.4062322",
"37.7517621 -122.4062364",
"37.9659228 -122.505765",
"37.6969336 -121.8650777",
"37.9457334 -122.3152847",
"38.1237134 -122.23956",
"37.8911093 -122.2813663",
"37.9067555 -122.0145836",
"38.0673887 -122.2185562",
"37.7807761 -122.2102385",
"37.8911093 -122.2813663")

to <- c("37.8454493 -122.2833803",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625",
"37.831217 -122.2719625")

DF <- cbind(from, to); DF <- as.data.frame(DF) # create data frame
DF$from <- as.character(DF$from) # mapdist demands input to be character type
DF$to <- as.character(DF$to) # mapdist demands input to be character type
remove (from, to) #remove input to avoid confusion

DF$row.number <- 1:nrow(DF) #create an index number for each row

循环 - 未评论
for (i in DF$row.number){
orig <- DF[i,c('from')]
dest <- DF[i,c('to')]
a <- mapdist(from = orig, to = dest, mode = "driving",output = "simple")
a$row.number <- i
DF$minutes[match(a$row.number, DF$row.number)] <- a$minutes
DF$hours[match(a$row.number, DF$row.number)] <- a$hours
DF$km[match(a$row.number, DF$row.number)] <- a$km
}

循环 - 带评论
for (i in DF$row.number){
orig <- DF[i,c('from')] # get origin from DF in the position line 'i', column 'from'
dest <- DF[i,c('to')] # get origin from DF in the position line 'i', column 'to'
a <- mapdist(from = orig, to = dest, mode = "driving",output = "simple") # create temp. df 'a' with the output from mapdist
a$row.number <- i # include in temp. df 'a' the index number of the row from DF
DF$minutes[match(a$row.number, DF$row.number)] <- a$minutes # use the index number as a matching key to input/update the value of the variable 'minutes'
DF$hours[match(a$row.number, DF$row.number)] <- a$hours # ibdem DF$km[match(a$row.number, DF$row.number)] <- a$km #ibdem
DF$miles[match(a$row.number, DF$row.number)] <- a$miles # ibdem
}

ps.1 - 请注意,您的代码没有从数据框中读取 O 和 D

ps.2 - 我相信通过结合 function 有一种更简单、更有效的方法来解决这个问题。和 lapply

关于r - ggmap mapdist 函数重复计算某些 O-D 对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25797580/

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