R - 使用国家,大陆,纬度和经度构建数据框

时间:2018-01-11 17:37:05

标签: r geocoding

我有一个数据框,其中包含一列国家/地区列表,我希望获得该国家/地区的大陆名称,纬度和经度。

例如,

df <- data.frame(CountryName = c("Austria", "Czech Republic", "Finland"))

我希望获得一个具有大陆,纬度和经度的数据框。

library(countrycode)
library(geocode)

df$continent <- as.factor(countrycode(sourcevar = df[, "CountryName"], origin = "country.name", destination = "continent"))

所以,我现在已经在我的数据框中使用了Continent。

geocode("Austria")在R中返回lat和long 你能指导我在Dataframe中获取它吗?

感谢。

2 个答案:

答案 0 :(得分:1)

原则上,您可以同时为所有国家/地区geocode()(我不知道包ggmap)致电geocode并使用cbind这样的内容:

cbind(df, ggmap::geocode(as.character(df[, "CountryName"]), force = FALSE))
# Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Austria&sensor=false
# Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Czech%20Republic&sensor=false
# Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Finland&sensor=false
#      CountryName continent      lon      lat
# 1        Austria    Europe 14.55007 47.51623
# 2 Czech Republic    Europe 15.47296 49.81749
# 3        Finland    Europe 25.74815 61.92411

然而,人们经常会收到有关OVER_QUERY_LIMIT的警告消息。为了解决这个问题,可能需要一个API密钥,c.f。 Getting OVER QUERY LIMIT after one request with geocode

答案 1 :(得分:0)

通过注册您自己的Google API密钥来解决“OVER QUERY LIMIT”问题:Google API sign-up

请参阅相关的堆栈溢出问题:stackoverflow.com/questions/36175529

相关问题