使用R

时间:2016-11-25 21:02:25

标签: r google-maps distance

数据有行= 434,来源= A列,目的地=“445 East 86th Street”,但它给了我一个错误:

Error in `$<-.data.frame`(`*tmp*`, "distance", value = list()) :
replacement has 0 rows, data has 434

我的代码是:

matching_factor <- function(origin){
   testData$distance<- list();
   xml.url <- paste0('http://maps.googleapis.com/maps/api/distancematrix/xml?origins=&#039;,curlEscape(urls=origin),'&destinations=',curlEscape(urls=testData$Address),'&mode=walking&sensor=false')
   xmlfile <- xmlParse(getURL(xml.url))
   dist <- xmlValue(xmlChildren(xpathApply(xmlfile,"//distance")[[1]])$value)
   distance <- as.numeric(sub(" miles","",dist))
   distance <- distance/1609.344
   testData$distance[['dist']] <- distance
return(testData$distance)
}

matching_factor( origin = "445 East 86th Street")

0 个答案:

没有答案