⚠️There's a newer version (1.0.5) of this package. Take me there.

tidygeocoder

A tidyverse-style geocoder interface for R. Utilizes US Census and Nominatim (OSM) geocoder services. Returns latitude and longitude in tibble format from addresses. You can find a demo I wrote up on R-Bloggers here.

Install

To install the stable version from CRAN (the official R package servers):

install.packages('tidygeocoder')

To install the development version from GitHub:

if(!require(devtools)) install.packages("devtools")
devtools::install_github("jessecambon/tidygeocoder",build_vignettes=TRUE)

Usage

In this brief example, we will use the US Census API to geocode some addresses in the sample_addresses dataset.

library(dplyr)
library(tidygeocoder)

lat_longs <- sample_addresses %>% 
  geocode(addr,lat=latitude,long=longitude)

Latitude and longitude columns are attached to our input dataset. Since we are using the US Census geocoder service, international locations and addresses which are not at the street level (such as cities) are not found.

nameaddrlatitudelongitude
White House1600 Pennsylvania Ave Washington, DC38.89875-77.03535
Transamerica Pyramid600 Montgomery St, San Francisco, CA 9411137.79470-122.40314
NAFake AddressNANA
NANANANA
NANA
US CityNashville,TNNANA
Willis Tower233 S Wacker Dr, Chicago, IL 6060641.87851-87.63666
International CityNairobi, KenyaNANA

Plot our geolocated points:

library(ggplot2)
library(maps)
library(ggrepel)
ggplot(lat_longs %>% filter(!is.na(longitude)),aes(longitude, latitude),color="grey98") +
  borders("state") + theme_classic() + geom_point() +
  theme(line = element_blank(),text = element_blank(),title = element_blank()) +
  geom_label_repel(aes(label =name),show.legend=F) +
  scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL)

To find international and non-street addresses, we must use the OSM service. We can use the ‘cascade’ method to attempt to use the US Census method for each address and only use the OSM service if the Census method fails (since OSM has a usage limit).

cascade_points <- sample_addresses %>% 
  geocode(addr,method='cascade')
nameaddrlatlonggeo_method
White House1600 Pennsylvania Ave Washington, DC38.898754-77.03535census
Transamerica Pyramid600 Montgomery St, San Francisco, CA 9411137.794700-122.40314census
NAFake AddressNANANA
NANANANANA
NANANA
US CityNashville,TN36.162230-86.77435osm
Willis Tower233 S Wacker Dr, Chicago, IL 6060641.878513-87.63666census
International CityNairobi, Kenya-1.28325336.81724osm

References

Copy Link

Version

Down Chevron

Install

install.packages('tidygeocoder')

Monthly Downloads

5,451

Version

0.2.5

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

March 22nd, 2020

Functions in tidygeocoder (0.2.5)