1

Say I have a vector of cities and countries, which may or may not include names of places that have since changed names:

locations <- c("Paris, France", "Sarajevo, Yugoslavia", "Rome, Italy", "Leningrad, Soviet Union", "St Petersburg, Russia")

The problem is that I can't use something like ggmap::geocode since it doesn't appear to work well for locations whose names have changed:

ggmap::geocode(locations, source = "dsk")

       lon      lat
1  2.34880 48.85341 #Works for Paris
2       NA       NA #Didn't work for Sarajevo
3 12.48390 41.89474 #Works for Rome
4 98.00000 60.00000 #Didn't work for the old name of St Petersburg  seems to just get the center of Russia
5 30.26417 59.89444 #Worked for St Petersburg

Is there an alternative functions I could use? If I have to "update" the names of the cities & countries, is there an easy method of going through this? I have hundreds of locations that I was looking to collect the longitude and latitude coordinates.

1 Answer 1

3

This might not be what you had in mind, but if you use the exact same code with only the city names (and not the countries), at least the two cases that you mentioned (Sarajevo and Leningrad) seem to work fine. You could try to run the function with a modified locations vector including just the city names, and see if you still get errors. Something like this:

(cities <- gsub(',.*', '', locations))

## [1] "Paris"         "Sarajevo"      "Rome"          "Leningrad"     "St Petersburg"

cbind(ggmap::geocode(cities, source = 'dsk'), cities)

##        lon      lat        cities
## 1  2.34880 48.85341         Paris
## 2 18.35644 43.84864      Sarajevo
## 3 12.48390 41.89474          Rome
## 4 30.26417 59.89444     Leningrad
## 5 30.26417 59.89444 St Petersburg
2
  • Interesting. One of the rare cases where less information is better. Thanks!
    – Phil
    Jul 31, 2017 at 0:26
  • 1
    Doesn't seem to work anymore. Error in ggmap::geocode(., source = "dsk") : datasciencetoolkit.org terminated its map service, sorry!
    – jay.sf
    Mar 15, 2019 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.