Learn Data Science for Business
Become a Data Scientist in our online courses
Earn 6 figures or more in 6 months or less by learning R, Shiny, Machine Learning, Time Series, Web Apps, AWS, Cloud, and more!
5-10 Hours Per Week. 80/20 Skills. End-To-End Business Projects.
Join over 100,000+ Data Scientists
How To Geocode In R For FREE
Written by Matt Dancho
What’s the one thing that helps you add value to your company’s raw geospatial data? GEOCODING.
Geocoding is the process of converting raw physical addresses to latitude and longitude geospatial points that can be viewed on a map and used for geospatial calculations. Heck - Geocoding has been known to increase my machine learning model performance by up to 10%!
Table of Contents
Today I’m going to show you how to do Geocoding in R for FREE using tidygeocoder. Here’s what you’re learning today:
- Tutorial Part 1: How to use
tidygeocoderto effortlessly geocode addresses (convert your company addresses to Lat/Long) - Tutorial Part 2: And I’m going to show you how to do Reverse Geocoding (go from Lat/Long to Physical Addresses)
- Bonus: I’m going to show you how to Map lat/long data using Simple Features + Mapview!
R-Tips Weekly
This article is part of R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks. Pretty cool, right?
Here are the links to get set up. 👇
This Tutorial is Available in Video
I have a companion video tutorial that gives you the bonus Mapview Shortcuts shown in this video (plus walks you through how to use it). And, I’m finding that a lot of my students prefer the dialogue that goes along with coding. So check out this video to see me running the code in this tutorial. 👇
Why Geocoding is a Must
Look, I’ve been working with customer data for a long time…
And one of the RICHEST sources of data is raw company addresses!
Think about it. If you know where a company is located, do you think that might be important to their purchasing behavior?
Well it was for me. In fact, I found out that just simply adding the Latitude and Longitude information to my customer churn prediction models…
Gave my models a 10% increase in performance!
Conclusion
You learned how to use the tidygeocoder library to geocode and reverse geocode. Great work! But, there’s a lot more to becoming a data scientist.
If you’d like to become a Business Data Scientist (and have an awesome career, improve your quality of life, enjoy your job, and all the fun that comes along), then I can help with that.
Tutorial: How to Geocode in R for Free with tidygeocoder
Time for geocoding with tidygeocoder. Let’s have some fun!
Step 1: Load the Libraries
Load the following libraries.
tidyverseandtidygeocoderare the main libraries.- But my bonus lat/long map hack uses
sfandmapview.
Step 2: Get My Pittsburgh Pharmacies Dataset
Next, you can steal my Pittsburgh Pharmacies dataset. This dataset is a great way to test your skills with Geocoding.
We’ll use the Pittsburgh Pharmacies dataset (171 geocoded pharmacies) throughout the rest of this tutorial.
Step 3: Geocode the Address Column to get Latitude and Longitude
Next, use the geocode() function to convert a company’s physical address to a Latitude / Longitude.
A quick point on the API being used. The default is method = "osm", which connects to the FREE Open Street Maps Nomenatim API. This is great, but may be too slow for your needs. Other free and paid APIs exist. (and yes google’s maps API is an option).
Step 4: Reverse Geocode to go from Lat/Long to Physical Address
Sometimes you have a latitude and longitude and want a physical address. For example, if your salesperson needs to know what addresses to visit (you wouldn’t send them a Lat/Long… or else they’d think you’re nuts!)
Did you know that you can reverse geocode?
You can! Here’s how to go from Latitude / Longitude to a Physical Address. (And save your inter-office reputation)