options(width = 100)

GitHub release R build status

This package uses Rcpp to quickly compute population/distance-weighted measures. Geodesic distances can be computed using either Haversine or Vincenty formulas. The package also has functions to return raw distance measures. If you are able to install Rcpp on your machine, you should be able to install this package and use these functions.

Install the latest development version from Github with

devtools::install_github("btskinner/distRcpp")

NB This package is still in early beta stages. It does not have much in the way of error handling. Data must be pre-processed so that no missing (NA) values are given to the functions.

Available functions

Weighted measures

dist_weighted_mean()

Interpolate values for a vector of locations (X) that are the inverse-distance-weighted average of measures taken at surrounding locations (Y). For each point, x, nearby values of the measure taken at Y are weighted more heavily than those from locations that are farther away.

popdist_weighted_mean()

Interpolate values for a vector of locations (X) that are the population/inverse-distance-weighted average of measures taken at surrounding locations (Y). For each point, x, nearby values of the measure taken at Y are weighted more heavily than those from locations that are farther away. Measures taken in more heavily populated y are given more weight than those with lower populations. This weighting scheme is a compromise between distance and population and is useful for interpolating measures that need to take both into account.

Distances

dist_1to1()

Compute and return the geodesic distance between two spatial points. Returns distance in meters.

dist_1tom()

Compute and return the geodesic distance between one location and a vector of other locations. Returns vector of distances in meters.

dist_mtom()

Compute and return the geodesic distance between each coordinate pair in two vectors. Returns n x k matrix of distances in meters, where n = # of locations in first vector and k = # of locations in second vector.

dist_df()

Compute distance between corresponding coordinate pairs and return vector of distances in meters. For use when creating a new data.frame or dplyr tbl_df() column.

dist_min()

Compute minimum distance between each starting point, x, and possible end points, Y. Returns vector of minimum distances in meters that equals # of starting points (size of X).

dist_max()

Compute maximum distance between each starting point, x, and possible end points, Y. Returns vector of maximum distances in meters that equals # of starting points (size of X).

Benchmark

Compare speed with base R function when measuring the distance between every United States population-weighted county centroid as measured in 2010 (N = 3,143 with complete measurements).

Load data

options(tidyverse.quiet = TRUE)
## libraries
libs <- c("tidyverse","microbenchmark","geosphere","distRcpp")
sapply(libs, require, character.only = TRUE)
## read data
df <- get(data(county_centers))
df

## subset to 2010 population-weighted centroids (pclon10, pclat10)
p <- df %>% select(pclon10, pclat10) %>% drop_na() %>% data.frame()

Check for equality

dist_R <- geosphere::distm(p, p, fun = distHaversine)
dist_Rcpp <- distRcpp::dist_mtom(p[,1],p[,2],p[,1],p[,2])

dist_R[1:5,1:5]
dist_Rcpp[1:5,1:5]
all.equal(dist_R, dist_Rcpp)

Benchmark

2018 MacBookPro, 2.9 GHz Intel Core i9, 32 GB 2400 MHz DDR4 SDRAM

microbenchmark(
    dist_R = geosphere::distm(p, p, fun = distHaversine),
    dist_Rcpp = distRcpp::dist_mtom(p[,1],p[,2],p[,1],p[,2]),
    times = 100
)

Big file

## get census block group centers of population
bg <- readr::read_csv("https://www2.census.gov/geo/docs/reference/cenpop2010/blkgrp/CenPop2010_Mean_BG.txt") %>%
    setNames(tolower(names(.))) %>%
    filter(statefp < 56) %>%
    mutate(id = paste0(statefp, countyfp, tractce, blkgrpce),
           lon = longitude,
           lat = latitude) %>%
    select(id, lon, lat) %>%
    drop_na()

ct <- get(data(county_centers)) %>%
    rename(id = fips,
           lon = pclon10,
           lat = pclat10) %>%
    drop_na()
bg
ct

system.time(dist_Rcpp <- distRcpp::dist_min(x_df = ct, y_df = bg))


btskinner/distRcpp documentation built on Dec. 10, 2020, 12:01 a.m.