Package {Rvoterdistance}


Type: Package
Title: Voter Distance to Polling Locations
Version: 2.0.0
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. Core computation uses the Haversine formula implemented in C++ via 'Rcpp'.
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
URL: https://github.com/lorenc5/Rvoterdistance
BugReports: https://github.com/lorenc5/Rvoterdistance/issues
Encoding: UTF-8
LazyData: true
Depends: R (≥ 4.0)
Imports: Rcpp (≥ 1.0.0)
Suggests: sf, testthat (≥ 3.0.0), knitr, rmarkdown
LinkingTo: Rcpp
RoxygenNote: 7.3.2
VignetteBuilder: knitr
Config/testthat/edition: 3
NeedsCompilation: yes
Packaged: 2026-05-20 16:32:55 UTC; lorencollingwood
Author: Loren Collingwood [aut, cre]
Maintainer: Loren Collingwood <lcollingwood@unm.edu>
Repository: CRAN
Date/Publication: 2026-05-27 20:00:02 UTC

Rvoterdistance: Voter Distance to Polling Locations

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. Core computation uses the Haversine formula implemented in C++ via 'Rcpp'.

Author(s)

Maintainer: Loren Collingwood lcollingwood@unm.edu

See Also

Useful links:


King County ballot drop box locations

Description

A data frame of ballot drop box locations in King County, Washington.

Usage

dbox

Format

A data frame with columns including lat and long.

Source

King County Elections


Calculate minimum distance in kilometers

Description

Given lat/lon vectors for voters and locations, returns the minimum Haversine distance in kilometers for each voter to the nearest location.

Usage

dist_km(lat1, lon1, lat2, lon2)

Arguments

lat1

Numeric vector of voter latitudes.

lon1

Numeric vector of voter longitudes.

lat2

Numeric vector of location latitudes.

lon2

Numeric vector of location longitudes.

Value

Numeric vector of minimum distances in kilometers.

Examples

data(meck_ev)
d <- dist_km(voter_meck$lat, voter_meck$long,
             early_meck$lat, early_meck$long)
summary(d)

Calculate minimum distance in miles

Description

Given lat/lon vectors for voters and locations, returns the minimum Haversine distance in miles for each voter to the nearest location.

Usage

dist_mile(lat1, lon1, lat2, lon2)

Arguments

lat1

Numeric vector of voter latitudes.

lon1

Numeric vector of voter longitudes.

lat2

Numeric vector of location latitudes.

lon2

Numeric vector of location longitudes.

Value

Numeric vector of minimum distances in miles.

Examples

data(meck_ev)
d <- dist_mile(voter_meck$lat, voter_meck$long,
               early_meck$lat, early_meck$long)
summary(d)

Mecklenburg County early voting locations

Description

A data frame of early voting locations in Mecklenburg County, North Carolina.

Usage

early_meck

Format

A data frame with columns including lat and long.

Source

Mecklenburg County Board of Elections


Haversine distance between two points

Description

Compute the Haversine (great-circle) distance between a single pair of lat/lon coordinates.

Usage

haversine(lat1, lon1, lat2, lon2, units = c("meters", "km", "miles"))

Arguments

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 "meters", "km", or "miles". Default "meters".

Value

Numeric scalar distance in the specified units.

Examples

# New York to London
haversine(40.7128, -74.0060, 51.5074, -0.1278, units = "km")

King County voter sample with geocoded addresses

Description

A sample of geocoded voter records from King County, Washington, including latitude and longitude of residential addresses.

Usage

king_geo

Format

A data frame with columns including Residence_Addresses_Latitude and Residence_Addresses_Longitude.

Source

King County voter file (anonymized sample)


Find nearest polling locations for each voter

Description

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.

Usage

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
)

Arguments

voters

A data frame, matrix, or sf POINT object containing voter locations. If a data frame or matrix, must contain lat/lon columns specified by voter_coords.

locations

A data frame, matrix, or sf POINT object containing polling/drop box locations. If a data frame or matrix, must contain lat/lon columns specified by location_coords.

voter_coords

Character vector of length 2: c("lat_col", "lon_col") identifying the latitude and longitude columns in voters. Ignored if voters is an sf object.

location_coords

Character vector of length 2: c("lat_col", "lon_col") identifying the latitude and longitude columns in locations. Ignored if locations is an sf object.

k

Integer. Number of nearest locations to return per voter. Default 1.

max_dist

Numeric or NULL. If not NULL, return all locations within this distance of each voter. Units controlled by units. Overrides k.

units

Character. One of "km", "miles", or "meters". Default "km".

append_data

Logical. If TRUE (default), include voter and matched location columns in the output. When k = 1, also appends the matched location row.

progress

Logical. If TRUE, print progress for large computations. Default FALSE.

Value

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.

Examples

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)

Mecklenburg County voter sample with geocoded addresses

Description

A sample of geocoded voter records from Mecklenburg County, North Carolina, including latitude and longitude.

Usage

voter_meck

Format

A data frame with columns including lat and long.

Source

Mecklenburg County voter file (anonymized sample)