0

I get a random number of errors that come back as NA when I try to geocode a lot of places using rgooglemaps getGeoCode function. Can anyone tell me why? (Reproducible code below)

library(RgoogleMaps)
library(foreach)
###Replicating a large search data###
PlaceVector <- c(rep("Anchorage,Alaska", 20), rep("Baltimore,Maryland", 20), 
rep("Birmingham,Alabama", 20))
iters <- length(PlaceVector)
###Looping to get each geocode###
geoadd <- foreach(a=1:iters, .combine=rbind) %do% {
  getGeoCode(paste(PlaceVector[a]))
}
geoadd <- as.data.frame(geoadd)
geoadd$Place <- PlaceVector

I get a random number of errors, usually around 15 where the latitude and longitudes in data frame geoadd come back as NA. I could loop it back on the NA's but that seems utterly inefficient. Do others have the same problem with the sample code provided?

Neal Barsch
  • 2,810
  • 2
  • 13
  • 39

1 Answers1

1

I get NA's in the example as well. I once had a problem with looping and geocoding. The problem was that I was hitting googleMaps to fast or with to many request within a minimum time frame. I built in a waiting period with Sys.sleep to solve the issue. The problem is finding the correct amount of microseconds to wait. This depends on your connection and response times of google.

phiver
  • 23,048
  • 14
  • 44
  • 56
  • Thanks phiver. What I ended up doing (worst fix in the world, and not guaranteed), I looped an if(is.na) thrice and in testing it seems to come up with full results. This would make sense as your fix implies the NA's would come apart from one another, which is also my experience. I'll look into the Sys.sleep method as well. Cheers. – Neal Barsch Jan 15 '18 at 00:36