【问题标题】:ggmap mapdist function repeating calculation for certain O-D pairsggmap mapdist 函数重复计算某些 O-D 对
【发布时间】:2014-11-06 00:01:06
【问题描述】:

我想使用 ggmap 中的 mapdist 函数查找行驶距离和时间。该函数适用于大多数以 Lat/Long 形式给出的 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")

【问题讨论】:

  • GMAPDist[!duplicated(GMAPDist),] 也许?否则,这应该是an issue 提交给维护者。
  • 我的回答能解决你的问题吗?

标签: r google-maps-api-3 ggmap


【解决方案1】:

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

输入数据

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
    }

循环 - 使用 cmets

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 - 我相信通过结合functionlapply有一种更简单、更有效的方法来解决这个问题

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    相关资源
    最近更新 更多