Find the number of nearby stores given a specific address or zip code

Hello, I have a problem in finding a package that can show the number of nearby stores at certain distances given a zip code. For example, given an address and its zip code , 1020 Main Campus Drive,
Room 2300A, Campus Box 7102 Raleigh, NC 27695-7102. I want to know the number of USPS within the distance of 5 or 10 miles of this location. Any suggestions or advice? Thanks in advance.

Google maps has an API that will return lon lat for addresses that can be read into a {sf} package data frame. Create one for USPS locations and one for target addresses.

Then, you can calculate distances with sf::st_distance(). Depending on the projection, the likelihood is that the distance will be in meters and it will be a straight-line distance, rather than a travel distance.


Continuing technocrat's post:

Once you register with Google for an API key at Google Maps Platform - Location and Mapping Solutions (requires a credit card, but I believe there is no charge for a small number of lookups as in assignments/projects, as opposed to using this frequently in business),

ggmap::register_google(key = "your key")

enter longitude, latitude

f <- c(-81.795350, 26.678110)
t <- c(-81.818920, 26.673490)
mapdist(from=f, to=t, mode = "driving")

gives driving distance in miles.

1 Like

@fcas80 is right; there is a free tier on the API. It's rate limited, so if you have a large number of lookups, you'll want to build in a delay every chunk of 25 or so for a second to keep within the limits.

Hello @technocrat, thanks for your help. However, I do not know the location of nearby USPS stores since I only know one address and its zip code. I need to figure out how many USPS stores are near it within a certain distance, say 5miles, only based on this specific location.

Hello @fcas80 , thanks for your help. However I only know this specific address and its zip code. I need to input its address and/or zip code with a certain miles, say 5 miles, to draw a circle with the location as a center, and then to find how many USPS stores within such a circle. Any other suggestions? Thanks so much for your help.

Go to and enter the target zip code and set search distance to 10 miles to find the 17 USPS locations

Hi @technocrat and @YuJiang ,
I tried scrapping the USPS PO locator website using the rvest package but its beyond my capabilities (although theoretically possible).
The database of USPS post office locations is available commercially (e.g. as a CSV file) which may be a solution, depending on cost constraints.

If it were me

  1. If this is a one-off, I'd cut and paste.
  2. If this is something to be done again occasionally, I would open a browser to copy all to a text editor to extract the good stuff. But that won't work for this site because the results are deeply buried in CSS and Javascript. About 99.5% of the results page is markup.
  3. If it were going to be done frequently, I'd look for a "spoofer" tool in some language that allows query scripting and capture.

Here's what I get out of five minutes manual work

150 WRENN DR CARY, NC 27511-9998
2009 FAIRVIEW RD RALEIGH, NC 27628-9800
205 S ACADEMY ST CARY, NC 27519-9998
2100 LAKE DAM RD RALEIGH, NC 27606-9981
2415 ATLANTIC AVE RALEIGH, NC 27604-1409
267 TIMBER DR GARNER, NC 27529-9998
2777 BRENTWOOD RD RALEIGH, NC 27604-9998
311 NEW BERN AVE RALEIGH, NC 27601-1417
3919 BERYL RD RALEIGH, NC 27607-9996
4901 DEPARTURE DR RALEIGH, NC 27616-9997
6320 CAPITAL BLVD STE 120 RALEIGH, NC 27616-3017
6508 HILBURN DR RALEIGH, NC 27613-1907

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.