0
我一直在嘗試2個郵政編碼之間的地圖距離。我有大約3萬條記錄。我瞭解谷歌只允許每天2500個查詢,因此我現在有一個來自他們的API。出於某種原因 - 我一直在努力將API密鑰插入代碼中。我還遇到了另一個名爲googleway的軟件包 - 它做了同樣的事情,但我喜歡mapdist的格式。有沒有一種方法:mapdist和googleway在R
一)使用API到這個代碼或
B)使用谷歌的方式包裝填充類似的結果從mapdist
欣賞提前您的支持。
library(ggmap)
library(plyr)
library(googleway)
key <- "XXX"
file_loc <- "C:/Users/Owner/Desktop/distance.csv"
x <- read.csv(file_loc, header = TRUE)
from <- x[1]
to <- x[2]
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 = c("simple", "all"), override_limit = FALSE)
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
DF$miles[match(a$row.number, DF$row.number)] <- a$miles
}
write.csv(DF, "newdata.csv") #Save dataset
使用'httr'直接調用API。另外,''''從''''''到'到'DF'位是循環的。 [讓您的示例可重現](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – alistaire