Geocoding function will not work, I keep getting the same error message

126 Views Asked by At
Alex %>%
  geocode(address = 'Address', method = "google",  lat = latitude, long = longitude)

Error in geocode(., address = "Address", method = "google", lat = latitude,  :
is.character(location) is not TRUE

I am trying to get coordinates for +17k addresses. The geocode function has worked fine before now but I realized I wasn't putting in the full address, I was only inputting the street address without city or state and getting wildly different coordinates.

For reproducibility I've got some random example data to test with and I've got it in 2 different datasets. One where the address is complete in one column and another dataset where the address is broken up into multiple columns for city, state, and zip code. I've tried both datasets and they both give me the exact same error code.

This dataset is called Alex.

ID Address
120 200 Walmart Way, Morehead, KY 40351
121 131 Jessica Lane, Olive Hill, KY 41164
122 125 Flemingsburg Rd, Morehead, KY 40351
library(tibble)
library(dplyr)
library(tidygeocoder)
library(ggmap)

Those are the packages I've got loaded, all of the columns in the Alex table are character type, not numeric, and the table itself is:

class(Alex)

[1] "tbl_df"     "tbl"        "data.frame"

I have a Google API key registered and nothing is working. Any help or insight would be greatly appreciated.

2

There are 2 best solutions below

2
defuneste On

this a solution using DeGAUSS

You will need to install docker (just follow https://www.docker.com/products/docker-desktop/ for your OS).

# this is easier for everyone to copy paste your example
alex <- data.frame(
  ID = c(120:124),
  Address = c("200 Walmart Way, Morehead, KY 40351",
        "131 Jessica Lane, Olive Hill, KY 41164",
        "125 Flemingsburg Rd, Morehead, KY 40351"
    

# I did not include the date, degauss prefer that you only use "id"
# and ""address"
 
names(alex) <- tolower(names(alex)) #  degauss is picky with address column name

# degauss take a csv as an argument
write.csv(alex, "alex.csv", row.names = FALSE)

# calling degauss container from R, system2 could have been nicer
system("docker run --rm -v $PWD:/tmp ghcr.io/degauss-org/geocoder:3.3.0 alex.csv")

# see the results
read.csv("alex_geocoder_3.3.0_score_threshold_0.5.csv")

One of the address was imprecise (the first one)

| ID | Address | latitude | longitude
| ---| ---------|----|----|
| 120 | 200 Walmart Way, Morehead, KY 40351 | 36.2014 | -83.4821 |
| 121 | 131 Jessica Lane, Olive Hill, KY 41164 | 38.2971| -83.1813|
| 122 | 125 Flemingsburg Rd, Morehead, KY 40351| 38.1779 | -83.4395 |
3
cristian-vargas On

I don't have a Google API key for replicating this, but using one of the other geocoding service provides, geocode(method="osm"), does return a valid result of your data, albeit OpenStreetMap Nominatum couldn't geocode every address.

# A tibble: 5 × 4
    ID Address                                     lat  long
  <int> <chr>                                     <dbl> <dbl>
1   200 Walmart Way, Morehead, KY 40351         38.2014 -83.4821 
2   131 Jessica Lane, Olive Hill, KY 41164      38.2971 -83.1813
3   125 Flemingsburg Rd, Morehead, KY 40351     38.1779 -83.4395

It might also be a conflict between the two of the packages you have loaded. ggmap also has a geocode() function with a named argument of location, which must be a character vector of street address or place names. Just to make sure, try specifying which geocode() function you want to use with tidygeocoder::geocode(address = "Address", method = "google", lat = latitude, long = longitude).

Edit: after getting a Google API key for their geocoding service and resolving the package conflict, the function returns (hopefully) accurate results, so the problem may lie somewhere upstream that isn't shown here?

alex <- data.frame(
  ID = c(120:124),
  Address = c(
    "200 Walmart Way, Morehead, KY 40351",
    "131 Jessica Lane, Olive Hill, KY 41164",
    "125 Flemingsburg Rd, Morehead, KY 40351",
  )
)

result <- geocode(
  alex,
  address = 'Address',
  method = 'google',
  lat = latitude,
  long = longitude
)

View(result)

Which then shows the following in the View pane:

ID Address latitude longitude
120 200 Walmart Way, Morehead, KY 40351 38.2014 -83.4821
121 131 Jessica Lane, Olive Hill, KY 41164 38.2971 -83.1813
122 125 Flemingsburg Rd, Morehead, KY 40351 38.1779 -83.4395