Search code examples
rfor-loopgeocode

for loop trough google geocode() but skip identical counties to reduce google API enquiries


Could someone please help me implementing an if statement in this for loop to skip identical country names. I.e. Spain is repeated several times in the example but one only needs to ask the API once, since there are restrictions on the google API service. However, I want to paste the lat long for Spain or other repeating names every time.

unfortunately, you need your own key or the code will not run.

register_google(key='your key')
t<-data.frame(importer_country=c("spain", "spain", "spain","united states","spain","eswatini", "spain", "spain", "spain", "spain", "spain", "spain", "spain"))
t$importer_country<-as.character(t$importer_country)
for(i in 1:nrow(t)){
  result <- geocode(t$importer_country[i], output = "latlon", source = "google")
  t$importer_lon[i] <- as.numeric(result[1])
  t$importer_lat[i] <- as.numeric(result[2])
}
head(t)

Solution

  • A straight forward solution is to create a new dataframe with the unique set of names. Pull the requested data and then merge the reduced dataframe's data with the original data set.

    #Create a subset dataframe with unique country names
    library(tibble)  #improved data frames
    reducedt <- tibble(importer_country = t[!duplicated(t$importer_country), ])
    
    #run function as is with reduced subset
    for(i in 1:nrow(reducedt)){
      result <- geocode(reducedt$importer_country[i], output = "latlon", source = "google")
      reducedt$importer_lon[i] <- as.numeric(result[1])
      reducedt$importer_lat[i] <- as.numeric(result[2])
    }
    
    #join the two data frames together.
    library(dplyr)
    finalnanswer <- left_join(t, reducedt)
    

    Also note that the geocode function is most likely vectorized thus you could probably avoid the need for the loop.

    Edit
    Vectorizing geocode() function. Use the most recent version of "ggmap" version >= 3.0.0.901 (there is a bug in an earlier version of the geocode function)

    reducedt <- tibble(importer_country = t[!duplicated(t$importer_country), ])
    
    result <- geocode(reducedt$importer_country, output = "latlon", source = "google")
    reducedt<-cbind(reducedt, result)
    
    finalnanswer <- left_join(t, reducedt)