View source: R/nearest_location.R
| nearest_location | R Documentation |
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
)
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.