1

I have a dataframe that has a list of countries on one column and I am looking to get the Continent Name, Latitude and Longitude of that country.

For example,

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

I am looking to get a dataframe that has Continent, latitude and longitude with it.

library(countrycode)
library(geocode)

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

So, I have got the Continent in my dataframe now.

geocode("Austria") returns lat and long in R. Can you guide me in getting that inside the Dataframe.

Thanks.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134

2 Answers2

1

In principle you can call geocode() from ggmap (I know no package geocode) for all countries at once and use cbind like this:

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

However, one quite often gets warning messages concerning OVER_QUERY_LIMIT. In order to solve that one probably needs an API key, c.f. Getting OVER QUERY LIMIT after one request with geocode.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
0

Get past the "OVER QUERY LIMIT" problem by signing up for your own Google API key: Google API sign-up

See the related stack-overflow question: stackoverflow.com/questions/36175529

RexBarker
  • 1,456
  • 16
  • 14