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 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
I got no long/lat data at all, only NA
Geocodes are Latitude & longitude Coordinates of a street address, which can be used to place markers on a map. Geocoding is the process of converting a street address into latitude/longitude coordinates, or geocodes, which can be used to place markers on a map.
Go to the Google Cloud Console. Click the Select a project button, then select the same project you set up for the Maps JavaScript API and click Open. From the list of APIs on the Dashboard, look for Geocoding API. If you see the API in the list, you're all set.
I have just found that this error message:
Error: is.character(location) is not TRUE
can be due to the address being encoded as a number, not a character. This can happen when you select from a data frame for instance, which was my case.
Do:
typeof(address)
and if it turns out to be numeric, change it to char
a2 <- as.character(address)
geocode(a2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With