R尝试查找欧洲城市的纬度/经度数据并获取地址解析错误信息 [英] R trying to find latitude/longitude data for cities in europe and getting geocode error messege
问题描述
我最近发布了一个问题,关于在欧洲城市中以地图上的点来绘制位置. 参见 R,获取经度/城市的纬度数据并将其添加到我的数据框中
I recently posted a question regarding plotting postions on cities in europe as points on a map. See R, get longitude/latitude data for cities and add it to my dataframe
cities xlsx文件包含欧洲大约2万个城市.
cities xlsx file contains about 20000 cities in europe.
尝试使用地理编码查找纬度/经度数据时收到错误消息.我在下面插入了部分代码:
I got an error message when trying to find the latitude/longitude data using geocode. I have inserted part of the code below:
cities <- read.xlsx("EU_city.xlsx",1)
# get frequencies
freq <- as.data.frame(table(cities))
library(plotrix)
freq$Freq <- rescale(freq$Freq, c(1,10)) # c(scale_min, scale_max)
# get cities latitude/longitude - kindly provided by google:
library(ggmap)
lonlat <- geocode(unique(cities))
cities <- cbind(freq, lonlat)
error message:
Error: is.character(location) is not TRUE
我猜我的数据框中的数据(城市)在地址解析调用中找不到. 如果在地理编码中不匹配
I guess the data(cities) in my dataframe is not found in the geocode call. Is there a way to ignore the city in the dtaframe if it is not matched in geocode
建议后问题的更新.......
Update of question after suggestion.......
尝试地理编码(例如字符(城市))
tried geocode(as.character(cities))
然后我的框架看起来像这样:
Then my frame looks like this:
> cities <- cbind(freq, lonlat)
> cities
cities Freq lon lat
1 ARNHEM 1.00 NA NA
2 ATHENS 3.25 NA NA
3 BAAR 1.00 NA NA
4 BAD VILBEL 1.00 NA NA
5 BILTHOVEN 1.00 NA NA
6 BOCHUM 10.00 NA NA
7 BREDA 3.25 NA NA
8 CAMBRIDGESHIRE 3.25 NA NA
9 DORDRECHT 1.00 NA NA
10 GAOETERSLOH 1.00 NA NA
11 GELSENKIRCHEN 1.00 NA NA
12 GOES 1.00 NA NA
13 GRONINGEN 3.25 NA NA
14 GUMMERSBACH-DIERINGHAUSEN 1.00 NA NA
15 HALSTEREN 1.00 NA NA
16 HANNOVER 1.00 NA NA
17 HARDERWIJK 1.00 NA NA
18 HEERLEN 3.25 NA NA
19 HILVERSUM 1.00 NA NA
我根本没有长/纬度数据,只有NA
I got no long/lat data at all, only NA
推荐答案
您只需要对cities
列进行地理编码(这有点令人困惑,因为您有一个名为cities
的数据框,其中有一个名为cities
).如有疑问,请尝试将其分解为较小的块.
You have to geocode just the cities
column (it's a little confusing that you have a data frame called cities
, and within it a column called cities
). When in doubt, try breaking things down into smaller chunks.
例如,一次尝试一个...
For example, try them one at a time ...
cities <- c("ARNHEM","ATHENS","BAAR","CAMBRIDGESHIRE")
library(ggmap)
geocode(cities[1])
## lon lat
## 1 5.89873 51.9851
geocode(cities[2])
## just checking ...
geocode("ATHENS GEORGIA")
## lon lat
## 1 -83.38333 33.95
现在一次尝试所有矢量:
Now try the vector all at once:
geocode(cities)
## lon lat
## 1 5.8987296 51.98510
## 2 23.7293097 37.98372
## 3 8.5286332 47.19585
## 4 0.0965375 52.27619
现在尝试使用数据框:
mydat <- read.csv(textConnection("
cities,Freq,lon,lat
ARNHEM,1.00,NA,NA
ATHENS,3.25,NA,NA
BAAR,1.00,NA,NA
BAD VILBEL,1.00,NA,NA
BILTHOVEN,1.00,NA,NA
BOGUS_PLACE,2,NA,NA"))
geocodes <- geocode(as.character(mydat$cities))
mydat <- data.frame(mydat[,1:2],geocodes)
## cities Freq lon lat
## 1 ARNHEM 1.00 5.898730 51.98510
## 2 ATHENS 3.25 23.729310 37.98372
## 3 BAAR 1.00 8.528633 47.19585
## 4 BAD VILBEL 1.00 8.739480 50.18234
## 5 BILTHOVEN 1.00 5.210381 52.13653
## 6 BOGUS_PLACE 2.00 -92.201158 44.49091
我不知道什么 BOGUS_PLACE
的结果意味着... !!
I don't know what the result for BOGUS_PLACE
means ...!!
这篇关于R尝试查找欧洲城市的纬度/经度数据并获取地址解析错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!