| Title: | Data & Functions for Working with US ZIP Codes |
|---|---|
| Description: | Make working with ZIP codes in R painless with an integrated dataset of U.S. ZIP codes and functions for working with them. Search ZIP codes by multiple geographies, including state, county, city & across time zones. Also included are functions for relating ZIP codes to Census data, geocoding & distance calculations. New analyses can select an immutable modern data bundle through the next-generation API, while the historical interface remains compatible with version 0.3.5 for reproducible research. |
| Authors: | Gavin Rozzi [aut, cre] (ORCID: <https://orcid.org/0000-0002-9969-8175>) |
| Maintainer: | Gavin Rozzi <[email protected]> |
| License: | GPL-3 |
| Version: | 0.4.1 |
| Built: | 2026-10-11 07:44:38 UTC |
| Source: | https://github.com/gavinrozzi/zipcoder |
The bundled zip_code_db is the lightweight ("simple") dataset. A
much larger companion database with detailed ACS demographic profiles per
ZIP code (the "comprehensive" database, ~450 MB SQLite) is published as a
checksum-pinned asset of a zipcodeR data release rather than shipped in the
package. Its public URL, checksum, and SQLite integrity have passed a
clean-machine smoke test.
download_comprehensive_data(force = FALSE)download_comprehensive_data(force = FALSE)
force |
If TRUE, re-download even if a verified copy is cached. |
This function downloads that database, verifies its SHA256 checksum,
and, on R 4.0 or newer, caches it under
tools::R_user_dir("zipcodeR", "data"); later calls return the cached
path immediately. On older supported R versions the verified file is kept
only in the session temporary directory. It never downloads without being
called explicitly. For offline use on R 4.0 or newer, copy the file to that
user data directory yourself (the expected file name is the asset name from
the data release).
Invisibly, the path to the downloaded SQLite database. Query it
with DBI/RSQLite, e.g.
DBI::dbConnect(RSQLite::SQLite(), download_comprehensive_data()).
## Not run: path <- download_comprehensive_data() ## End(Not run)## Not run: path <- download_comprehensive_data() ## End(Not run)
This function is retained without behavioral changes for reproducibility
with zipcodeR 0.3.5. It attempts to modify files inside an installed package
and resolves an upstream "latest" release, so it is unsuitable for a pinned
research workflow. New work should use download_zip_data_bundle() or
read_zip_data_bundle() and pass the resulting object explicitly to the
_ng API. No runtime warning is emitted because warnings are part of the
frozen legacy contract.
download_zip_data(force = FALSE)download_zip_data(force = FALSE)
force |
Boolean, if set to TRUE will force overwrite existing data files with new version |
Data files needed for package functionality, stored in data directory of package install
## Not run: download_zip_data() ## End(Not run)## Not run: download_zip_data() ## End(Not run)
Downloads one explicitly requested data release, verifies its checksum, and
caches it in the user's data directory on R 4.0 or newer. On older supported
R versions, CRAN policy does not permit a package-managed persistent cache,
so the verified file is retained only for the current R session. The
function never resolves aliases such as "latest", and no lookup function
downloads data implicitly.
download_zip_data_bundle(version, force = FALSE)download_zip_data_bundle(version, force = FALSE)
version |
Exact registered data version, for example |
force |
Re-download and replace a verified cached copy. |
A validated zipcodeR_data_bundle.
Returns that lat / lon pair of the centroid of a given ZIP code
geocode_zip(zip_code)geocode_zip(zip_code)
zip_code |
A 5-digit U.S. ZIP code |
tibble of lat lon coordinates
geocode_zip("07762") geocode_zip("90210") geocode_zip("90210")$lat geocode_zip("90210")$lnggeocode_zip("07762") geocode_zip("90210") geocode_zip("90210")$lat geocode_zip("90210")$lng
Geocode ZIP codes using an explicit data bundle
geocode_zip_ng(data, zip_code)geocode_zip_ng(data, zip_code)
data |
A |
zip_code |
ZIP code(s). |
One coordinate row per input, preserving order and duplicates.
Get all congressional districts for a given ZIP code
get_cd(zip_code)get_cd(zip_code)
zip_code |
A U.S. ZIP code |
a named list of two-digit state code and two digit district code
get_cd("08731") get_cd("90210")get_cd("08731") get_cd("90210")
Get congressional districts using an explicit data bundle
get_cd_ng(data, zip_code)get_cd_ng(data, zip_code)
data |
A |
zip_code |
One five-digit ZIP code. |
A named list with parallel state_fips and district vectors.
Get all Census tracts within a given ZIP code
get_tracts(zip_code)get_tracts(zip_code)
zip_code |
A U.S. ZIP code |
tibble of Census tracts and data from Census crosswalk file found for given ZIP code
get_tracts("08731") get_tracts("90210")get_tracts("08731") get_tracts("90210")
Get Census tracts using an explicit data bundle
get_tracts_ng(data, zip_code)get_tracts_ng(data, zip_code)
data |
A |
zip_code |
One five-digit ZIP or ZCTA code. |
A tract crosswalk tibble.
Returns true if the given ZIP code is also a ZIP code tabulation area (ZCTA)
is_zcta(zip_code)is_zcta(zip_code)
zip_code |
A 5-digit U.S. ZIP code |
Boolean TRUE or FALSE based upon whether provided ZIP code is a ZCTA by testing whether it exists in the U.S. Census crosswalk data
is_zcta("90210") is_zcta("99999") is_zcta("07762")is_zcta("90210") is_zcta("99999") is_zcta("07762")
Test ZCTA membership using an explicit data bundle
is_zcta_ng(data, zip_code)is_zcta_ng(data, zip_code)
data |
A |
zip_code |
ZIP code(s). |
A logical vector.
Normalize ZIP codes
normalize_zip(zipcode)normalize_zip(zipcode)
zipcode |
messy ZIP code to be normalized |
Normalized zipcode
normalize_zip(0008731)normalize_zip(0008731)
Normalize ZIP codes with corrected boundary behavior
normalize_zip_ng(zipcode)normalize_zip_ng(zipcode)
zipcode |
Character or numeric ZIP values. |
Normalized five-character ZIP values.
Reads and validates a previously downloaded zipcodeR data bundle. Bundles
are explicit, immutable inputs for the next-generation (_ng) API; reading
one never changes the datasets used by the legacy API.
read_zip_data_bundle(path, sha256 = NULL)read_zip_data_bundle(path, sha256 = NULL)
path |
Path to a bundle |
sha256 |
Optional expected SHA256 checksum. Supplying the checksum is
strongly recommended when the file did not come from
|
An object of class zipcodeR_data_bundle.
Given a ZIP code, returns columns of metadata about that ZIP code
reverse_zipcode(zip_code)reverse_zipcode(zip_code)
zip_code |
A 5-digit U.S. ZIP code or chracter vector with multiple ZIP codes |
A tibble containing data for the ZIP code(s)
reverse_zipcode("90210") reverse_zipcode("08731") reverse_zipcode(c("08734", "08731")) reverse_zipcode("07762")$county reverse_zipcode("07762")$statereverse_zipcode("90210") reverse_zipcode("08731") reverse_zipcode(c("08734", "08731")) reverse_zipcode("07762")$county reverse_zipcode("07762")$state
Reverse-geocode ZIP codes using an explicit data bundle
reverse_zipcode_ng(data, zip_code)reverse_zipcode_ng(data, zip_code)
data |
A |
zip_code |
Five-digit ZIP code(s). |
One row per input, in input order, with duplicates preserved. The
result adds state_fips (two digits) and county_fips (the full
five-digit county identifier) for the database's predominant county.
Get all ZIP codes that fall within a given congressional district
search_cd(state_fips_code, congressional_district)search_cd(state_fips_code, congressional_district)
state_fips_code |
A two-digit U.S. FIPS code for a state |
congressional_district |
A two digit number specifying a congressional district in a given |
tibble of all congressional districts found for given ZIP code, including state code
search_cd("34", "03") search_cd("36", "05")search_cd("34", "03") search_cd("36", "05")
Search a congressional district using an explicit data bundle
search_cd_ng(data, state_fips_code, congressional_district)search_cd_ng(data, state_fips_code, congressional_district)
data |
A |
state_fips_code |
One state FIPS code. |
congressional_district |
One two-digit district code. |
A ZIP-to-district tibble.
Search ZIP codes for a given city within a state
search_city(city_name, state_abb)search_city(city_name, state_abb)
city_name |
Name of major city to search |
state_abb |
Two-digit code for a U.S. state |
tibble of all ZIP code data found for given city
search_city("Spring Lake", "NJ") search_city("Chappaqua", "NY")search_city("Spring Lake", "NJ") search_city("Chappaqua", "NY")
Search a city using an explicit data bundle
search_city_ng(data, city_name, state_abb)search_city_ng(data, city_name, state_abb)
data |
A |
city_name |
City name. |
state_abb |
Two-letter state abbreviation. |
A tibble from the selected data vintage.
Search ZIP codes for a county
search_county(county_name, state_abb, ...)search_county(county_name, state_abb, ...)
county_name |
Name of a county within a U.S. state |
state_abb |
Two-digit code for a U.S. state |
... |
if the parameter similar = TRUE, then send the parameter max.distance to the base function agrep. Default is 0.1. |
tibble of all ZIP codes for given county name
middlesex <- search_county("Middlesex", "NJ") alameda <- search_county("alameda", "CA") search_county("ST BERNARD", "LA", similar = TRUE)$zipcodemiddlesex <- search_county("Middlesex", "NJ") alameda <- search_county("alameda", "CA") search_county("ST BERNARD", "LA", similar = TRUE)$zipcode
Search a county using an explicit data bundle
search_county_ng(data, county_name, state_abb, ...)search_county_ng(data, county_name, state_abb, ...)
data |
A |
county_name |
County name. |
state_abb |
Two-letter state abbreviation. |
... |
Set |
A tibble from the selected data vintage.
Returns all ZIP codes found within a given FIPS code
search_fips(state_fips, county_fips)search_fips(state_fips, county_fips)
state_fips |
A U.S. FIPS code |
county_fips |
A 1-3 digit county FIPS code (optional) |
tibble of Census tracts and data from Census crosswalk file found for given ZIP code
search_fips("34") search_fips("34", "03") search_fips("34", "3") search_fips("36", "003")search_fips("34") search_fips("34", "03") search_fips("34", "3") search_fips("36", "003")
Search FIPS codes using an explicit data bundle
search_fips_ng(data, state_fips, county_fips)search_fips_ng(data, state_fips, county_fips)
data |
A |
state_fips |
One state FIPS code. |
county_fips |
Optional one- to three-digit county FIPS code. |
A tibble from the selected data vintage.
Search for ZIP codes that are within a given radius from a point
search_radius(lat, lng, radius = 1)search_radius(lat, lng, radius = 1)
lat |
latitude |
lng |
longitude |
radius |
distance to search in miles, set by default to 1 |
a tibble containing the ZIP code(s) within the provided radius and distance from the provided coordinates in miles
## Not run: search_radius(39.9, -74.3, 10) ## End(Not run)## Not run: search_radius(39.9, -74.3, 10) ## End(Not run)
Search within a radius using an explicit data bundle
search_radius_ng(data, lat, lng, radius = 1)search_radius_ng(data, lat, lng, radius = 1)
data |
A |
lat, lng
|
Query coordinates. |
radius |
Radius in miles. |
A tibble of ZIP codes and haversine distances.
Search for ZIP codes located within a given state
search_state(state_abb)search_state(state_abb)
state_abb |
Two-digit code representing a U.S. state |
tibble of all ZIP codes for each state code defined in state_abb
search_state("NJ") search_state(c("NJ", "NY", "CT"))search_state("NJ") search_state(c("NJ", "NY", "CT"))
Search a state using an explicit data bundle
search_state_ng(data, state_abb)search_state_ng(data, state_abb)
data |
A |
state_abb |
Two-letter state abbreviation(s). |
A tibble grouped in query order; repeated states repeat their rows.
Search all ZIP codes located within a given timezone
search_tz(tz)search_tz(tz)
tz |
Timezone |
tibble of all ZIP codes found for given timezone
eastern <- search_tz("Eastern") pacific <- search_tz("Mountain")eastern <- search_tz("Eastern") pacific <- search_tz("Mountain")
Search a timezone using an explicit data bundle
search_tz_ng(data, tz)search_tz_ng(data, tz)
data |
A |
tz |
Timezone value(s). |
A tibble grouped in query order; repeated timezones repeat their rows.
A dataset containing the relationships between ZIP code tabulation areas (ZCTA) and Census Tracts. This contains selected variables from the official crosswalk file.
zcta_crosswalkzcta_crosswalk
A data frame with 148897 rows and 3 variables:
2010 ZIP Code Tabulation Area
2010 Census Tract Code
Concatenation of 2010 State, County, and Tract
This is the exact dataset distributed by zipcodeR 0.3.5. It is intentionally
frozen so legacy calls remain reproducible; newer versioned relationships
are available only through an explicit data bundle and the _ng API.
https://www.census.gov/geographies/reference-files/time-series/geo/relationship-files.html
A dataset containing detailed information for U.S. ZIP codes
zip_code_dbzip_code_db
A data frame with 41877 rows and 24 variables:
5 digit U.S. ZIP code
ZIP category such as Standard, PO Box, or Unique
Major city serving the ZIP code
City of post office serving the ZIP code
List of common cities represented by the ZIP code
Name of county containing the ZIP code
Two-digit state code for ZIP code location
Latitude of the centroid for the ZIP code
Longitude of the centroid for the ZIP code
Timezone of the ZIP code
Radius of the ZIP code in miles
List of area codes for telephone numbers within this ZIP code
Total population of the ZIP code
Population density of the ZIP code (persons per square mile)
Area of the land contained within the ZIP code in square miles
Area of the waters contained within the ZIP code in square miles
Number of housing units within the ZIP code
Number of housing units within the ZIP code
Median home price within the ZIP code
Median household income within the ZIP code
Bounding box coordinates
Bounding box coordinates
Bounding box coordinates
Bounding box coordinates
This is the exact 41,877-row database distributed by zipcodeR 0.3.5,
including its historical values and limitations. It is intentionally frozen
for research reproducibility. Use an explicit data bundle with _ng
functions for corrected behavior and a pinned source manifest.
https://github.com/MacHu-GWU/uszipcode-project/files/5183256/simple_db.log
Inspect the provenance supplied with a zipcodeR data bundle
zip_data_provenance(bundle, dataset = NULL, key = NULL)zip_data_provenance(bundle, dataset = NULL, key = NULL)
bundle |
A |
dataset |
Optional dataset name ( |
key |
Optional record key (ZIP or ZCTA) to select. |
A provenance data frame combining source/method records with record-level quality status and reasons.
With no argument, reports the immutable data bundled with the legacy API. Given a downloaded data bundle, reports that bundle's metadata and verified checksum.
zip_data_version(x = NULL)zip_data_version(x = NULL)
x |
|
A named metadata list.
Calculate the distance between two ZIP codes in miles
zip_distance(zipcode_a, zipcode_b, lonlat = TRUE, units = "miles")zip_distance(zipcode_a, zipcode_b, lonlat = TRUE, units = "miles")
zipcode_a |
First vector of ZIP codes |
zipcode_b |
Second vector of ZIP codes |
lonlat |
lonlat argument to pass to raster::pointDistance() to select method of distance calculation. Default is TRUE to calculate distance over a spherical projection. FALSE will calculate the distance in Euclidean (planar) space. |
units |
Specify which units to return distance calculations in. Choices include meters or miles. |
a data.frame containing a column for each ZIP code and a new column containing the distance between the two columns of ZIP code
zip_distance("08731", "08901")zip_distance("08731", "08901")
Calculate ZIP-to-ZIP distance using an explicit data bundle
zip_distance_ng( data, zipcode_a, zipcode_b, lonlat = TRUE, units = c("miles", "meters") )zip_distance_ng( data, zipcode_a, zipcode_b, lonlat = TRUE, units = c("miles", "meters") )
data |
A |
zipcode_a, zipcode_b
|
ZIP vectors. |
lonlat |
Use great-circle haversine distance; |
units |
|
A data frame of paired ZIP codes and distances.
A dataset containing mappings between ZIP codes and congressional districts
zip_to_cdzip_to_cd
A data frame with 45914 rows and 2 variables:
5 digit U.S. ZIP code
Four digit congressional district code (State FIPS code + district number)
This is the exact pre-2020 HUD-USPS-derived mapping shipped in zipcodeR 0.3.5. It is frozen for legacy reproducibility and should not be interpreted as a current redistricting vintage. Current authoritative ZCTA relationships are available in explicit data bundles.
Historical HUD-USPS-derived snapshot archived at https://github.com/gavinrozzi/zipcodeR-data