2016-07-26 8 views
0

나는 2 개의 엽서 사이의 거리를 측정 해왔다. 약 ~ 30,000 개의 레코드가 있습니다. Google은 하루에 2,500 건의 쿼리 만 허용하므로 API가 제공됩니다. 어떤 이유로 API 키를 코드에 삽입하는 데 어려움을 겪고 있습니다. 나도 googleway라고하는 또 다른 패키지를 발견했다 - 나는 mapdist의 형식을 좋아하지만 똑같은 일을한다. 방법이 있나요 :mapdist 및 googleway in R

A))이 코드 또는
B로 API를 사용

사전에 지원을 감사합니다 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 
+0

API를 직접 호출하려면 httr을 사용하십시오. 또한, 'x'에서 'to'/ 'to'to 'DF'비트는 원으로 이어집니다. 그리고 [귀하의 모범을 재현 가능하게하십시오] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – alistaire

답변

1

지금까지 내가 googleway에 관해서는 ggmap::mapdist

와 API 키를 지정할 수 없습니다 당신을 말할 수있는, 그것은

key <- "your_api_key" 
google_distance(origins = list("MCG, Melbourne, Australia"), 
       destinations = list("Sydney Opera House, Australia"), 
       key = key) 

# $destination_addresses 
# [1] "Sydney NSW, Australia" 
# 
# $origin_addresses 
# [1] "Jolimont Station, Wellington Cres, East Melbourne VIC 3002, Australia" 
# 
# $rows 
# elements 
# 1 869 km, 869270, 8 hours 51 mins, 31847, 8 hours 45 mins, 31485, OK 
# 
# $status 
# [1] "OK" 

입니다 사용하는 방법 (I 패키지를 썼다) from/to 주소의 data.frame이있는 경우 :

df <- data.frame(from = c("MCG, Melbourne, Australia", "Sydney Opera House, Australia"), 
       to = c("Sydney Opera House, Australia", "Canberra, Australia")) 


res <- apply(df, 1, function(x){ 

    google_distance(origins = list(x["from"]), 
        destinations = list(x["to"]), 
        key = key) 

}) 
res 
# [[1]] 
# [[1]]$destination_addresses 
# [1] "Sydney NSW, Australia" 
# 
# [[1]]$origin_addresses 
# [1] "Jolimont Station, Wellington Cres, East Melbourne VIC 3002, Australia" 
# 
# [[1]]$rows 
# elements 
# 1 869 km, 869270, 8 hours 51 mins, 31847, 8 hours 44 mins, 31451, OK 
# 
# [[1]]$status 
# [1] "OK" 
# 
# 
# [[2]] 
# [[2]]$destination_addresses 
# [1] "Canberra ACT 2601, Australia" 
# 
# [[2]]$origin_addresses 
# [1] "Sydney NSW, Australia" 
# 
# [[2]]$rows 
# elements 
# 1 286 km, 286143, 3 hours 1 min, 10859, 3 hours 6 mins, 11152, OK 
# 
# [[2]]$status 
# [1] "OK"