假设我有一个城市和国家/地区的向量,其中可能包含或不包含已更改名称的地点名称:
locations <- c("Paris, France", "Sarajevo, Yugoslavia", "Rome, Italy", "Leningrad, Soviet Union", "St Petersburg, Russia")
问题在于我不能使用类似ggmap::geocode
的内容,因为它似乎不适用于名称已更改的位置:
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
我可以使用其他功能吗?如果我必须“更新”城市名称和国家,是否有一种简单的方法可以解决这个问题?我有数百个地点可以收集经度和纬度坐标。
答案 0 :(得分:3)
这可能不是你的想法,但如果你只使用城市名称(而不是国家)使用完全相同的代码,至少你提到的两个案例(萨拉热窝和列宁格勒)似乎工作正常。您可以尝试使用修改后的locations
向量运行该函数,仅包含城市名称,并查看是否仍然出现错误。像这样:
(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