0

Data has rows = 434, origin = column A, destination = “445 East 86th Street”, but it is giving me an error :

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

My code is :

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")
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete, and verifiable example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Perhaps the following tips on [asking a good question](http://stackoverflow.com/help/how-to-ask) may also be worth a read. – lmo Nov 25 '16 at 21:03
  • Starts out looking wrong: `testData$distance<- list();`. What structure is testData and why are you trying to alter an object outside of a function? What was that supposed to do? This `testData$distance[['dist']] <- distance` also looks really unlikely to succeed. Think you need to do more basic R tutorials. – IRTFM Nov 25 '16 at 21:23

0 Answers0