| Title: | Voter Distance to Polling Locations and Geographic Boundaries |
|---|---|
| Description: | Calculates the distance between each voter in a voter file (given lat/long coordinates or sf point geometries) and multiple polling or vote-by-mail drop box locations. Returns nearest location, k-nearest locations, or all locations within a distance threshold. Also computes minimum distance from voters to geographic boundaries such as rivers, state borders, or district lines provided as 'sf' line or polygon geometries. Core computation uses the Haversine formula and spherical cross-track distance implemented in C++ via 'Rcpp'. |
| Authors: | Loren Collingwood [aut, cre] |
| Maintainer: | Loren Collingwood <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 2.1.0 |
| Built: | 2026-05-29 19:41:53 UTC |
| Source: | https://github.com/lorenc5/rvoterdistance |
A data frame of ballot drop box locations in King County, Washington.
dboxdbox
A data frame with columns including lat and long.
King County Elections
Given lat/lon vectors for voters and locations, returns the minimum Haversine distance in kilometers for each voter to the nearest location.
dist_km(lat1, lon1, lat2, lon2)dist_km(lat1, lon1, lat2, lon2)
lat1 |
Numeric vector of voter latitudes. |
lon1 |
Numeric vector of voter longitudes. |
lat2 |
Numeric vector of location latitudes. |
lon2 |
Numeric vector of location longitudes. |
Numeric vector of minimum distances in kilometers.
data(meck_ev) d <- dist_km( voter_meck$lat, voter_meck$long, early_meck$lat, early_meck$long ) summary(d)data(meck_ev) d <- dist_km( voter_meck$lat, voter_meck$long, early_meck$lat, early_meck$long ) summary(d)
Given lat/lon vectors for voters and locations, returns the minimum Haversine distance in miles for each voter to the nearest location.
dist_mile(lat1, lon1, lat2, lon2)dist_mile(lat1, lon1, lat2, lon2)
lat1 |
Numeric vector of voter latitudes. |
lon1 |
Numeric vector of voter longitudes. |
lat2 |
Numeric vector of location latitudes. |
lon2 |
Numeric vector of location longitudes. |
Numeric vector of minimum distances in miles.
data(meck_ev) d <- dist_mile( voter_meck$lat, voter_meck$long, early_meck$lat, early_meck$long ) summary(d)data(meck_ev) d <- dist_mile( voter_meck$lat, voter_meck$long, early_meck$lat, early_meck$long ) summary(d)
Computes the minimum great-circle distance from each voter to the
nearest point on a boundary line or polygon edge. The boundary can
represent a river, state border, or any other geographic feature
provided as an sf geometry object.
dist_to_boundary( voters, boundary, voter_coords = NULL, units = c("km", "miles", "meters"), progress = TRUE )dist_to_boundary( voters, boundary, voter_coords = NULL, units = c("km", "miles", "meters"), progress = TRUE )
voters |
A data frame, matrix, or |
boundary |
An |
voter_coords |
Character vector of length 2 giving the column
names for latitude and longitude in |
units |
One of |
progress |
Logical; show progress messages? Default |
For polygon inputs the distance is measured to the polygon's boundary (perimeter), not to its interior. A point inside the polygon returns the positive distance to the nearest edge.
Core computation uses the spherical cross-track distance formula implemented in C++ for performance, with bounding-box pruning to skip distant segments.
Numeric vector of distances (one per voter) in the requested units.
## Not run: library(sf) # Create a simple north-south boundary line border <- st_sf( geometry = st_sfc( st_linestring(matrix(c(-109.05, 31.33, -109.05, 37.0), ncol = 2, byrow = TRUE )), crs = 4326 ) ) voters <- data.frame(lat = c(35.08, 32.0), lon = c(-106.65, -108.5)) dist_to_boundary(voters, border, voter_coords = c("lat", "lon")) ## End(Not run)## Not run: library(sf) # Create a simple north-south boundary line border <- st_sf( geometry = st_sfc( st_linestring(matrix(c(-109.05, 31.33, -109.05, 37.0), ncol = 2, byrow = TRUE )), crs = 4326 ) ) voters <- data.frame(lat = c(35.08, 32.0), lon = c(-106.65, -108.5)) dist_to_boundary(voters, border, voter_coords = c("lat", "lon")) ## End(Not run)
A data frame of early voting locations in Mecklenburg County, North Carolina.
early_meckearly_meck
A data frame with columns including lat and long.
Mecklenburg County Board of Elections
Compute the Haversine (great-circle) distance between a single pair of lat/lon coordinates.
haversine(lat1, lon1, lat2, lon2, units = c("meters", "km", "miles"))haversine(lat1, lon1, lat2, lon2, units = c("meters", "km", "miles"))
lat1 |
Latitude of point 1 (degrees). |
lon1 |
Longitude of point 1 (degrees). |
lat2 |
Latitude of point 2 (degrees). |
lon2 |
Longitude of point 2 (degrees). |
units |
One of |
Numeric scalar distance in the specified units.
# New York to London haversine(40.7128, -74.0060, 51.5074, -0.1278, units = "km")# New York to London haversine(40.7128, -74.0060, 51.5074, -0.1278, units = "km")
A sample of geocoded voter records from King County, Washington, including latitude and longitude of residential addresses.
king_geoking_geo
A data frame with columns including Residence_Addresses_Latitude
and Residence_Addresses_Longitude.
King County voter file (anonymized sample)
Calculates the distance between each voter and a set of polling/drop box locations using the Haversine formula. Can return the single nearest location, the k nearest, or all locations within a distance threshold.
nearest_location( voters, locations, voter_coords = NULL, location_coords = NULL, k = 1L, max_dist = NULL, units = c("km", "miles", "meters"), append_data = TRUE, progress = FALSE )nearest_location( voters, locations, voter_coords = NULL, location_coords = NULL, k = 1L, max_dist = NULL, units = c("km", "miles", "meters"), append_data = TRUE, progress = FALSE )
voters |
A data frame, matrix, or |
locations |
A data frame, matrix, or |
voter_coords |
Character vector of length 2: |
location_coords |
Character vector of length 2:
|
k |
Integer. Number of nearest locations to return per voter.
Default |
max_dist |
Numeric or |
units |
Character. One of |
append_data |
Logical. If |
progress |
Logical. If |
A data frame. If k = 1 and max_dist is NULL: one row per
voter with distance columns (distance_m, distance_km,
distance_miles). If k > 1 or max_dist is not NULL: one row per
voter-location pair with a rank column.
data(meck_ev) # Nearest single location for each voter result <- nearest_location(voter_meck, early_meck, voter_coords = c("lat", "long"), location_coords = c("lat", "long") ) head(result) # 3 nearest locations per voter result_k3 <- nearest_location(voter_meck, early_meck, voter_coords = c("lat", "long"), location_coords = c("lat", "long"), k = 3 ) head(result_k3) # All locations within 10 km result_10km <- nearest_location(voter_meck, early_meck, voter_coords = c("lat", "long"), location_coords = c("lat", "long"), max_dist = 10, units = "km" ) head(result_10km)data(meck_ev) # Nearest single location for each voter result <- nearest_location(voter_meck, early_meck, voter_coords = c("lat", "long"), location_coords = c("lat", "long") ) head(result) # 3 nearest locations per voter result_k3 <- nearest_location(voter_meck, early_meck, voter_coords = c("lat", "long"), location_coords = c("lat", "long"), k = 3 ) head(result_k3) # All locations within 10 km result_10km <- nearest_location(voter_meck, early_meck, voter_coords = c("lat", "long"), location_coords = c("lat", "long"), max_dist = 10, units = "km" ) head(result_10km)
A sample of geocoded voter records from Mecklenburg County, North Carolina, including latitude and longitude.
voter_meckvoter_meck
A data frame with columns including lat and long.
Mecklenburg County voter file (anonymized sample)