inst/doc/distance_objects.R

## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = FALSE, # hide code
  message = FALSE, # hide messages
  warning = FALSE, # hide warnings
  fig.width = 12,
  fig.height = 7,
  out.width = "100%"
)

## ----setup--------------------------------------------------------------------
library(gsClusterDetect)

## ----eval=TRUE, echo=TRUE-----------------------------------------------------
minnesota_counties <- county_distance_matrix(st = "MN")

# show the length and first 10 values of the `loc_vec`
length(minnesota_counties$loc_vec)
minnesota_counties$loc_vec |> head(10)

# show the dimension and upper left section of the `distance_matrix`
dim(minnesota_counties$distance_matrix)
minnesota_counties$distance_matrix[1:5, 1:5]

## ----eval=TRUE, echo=TRUE-----------------------------------------------------
maryland_zips <- zip_distance_matrix(st = "MD")

# show the length and first 10 values of the `loc_vec`
length(maryland_zips$loc_vec)
maryland_zips$loc_vec |> head(10)

# show the dimension and upper left section of the `distance_matrix`
dim(maryland_zips$distance_matrix)
maryland_zips$distance_matrix[1:5, 1:5]

## ----echo=TRUE, eval=TRUE-----------------------------------------------------
cook_county_tracts <- tract_distance_matrix(
  st = "IL",
  county = "031" # the full 5-digit code for Cook County, IL is 17031
)

# show the length and first 10 values of the `loc_vec`
length(cook_county_tracts$loc_vec)
cook_county_tracts$loc_vec |> head(10)

# show the dimension and upper left section of the `distance_matrix`
dim(cook_county_tracts$distance_matrix)
cook_county_tracts$distance_matrix[1:5, 1:5]

## ----echo=TRUE, eval=FALSE----------------------------------------------------
# maryland_zip <- zip_distance_matrix(st = "MD", unit = "kilometers")

## ----echo=TRUE, eval=TRUE-----------------------------------------------------
# Use the built-in-counties dataset to get a dataframe of
# counties in the states of interests
states <- c("Delaware", "Maryland", "Virginia")
delmarva_counties <- counties[state_name %in% states]

head(delmarva_counties, 3)

# Use the custom function to get the distance matrix
delmarva_dm <- custom_distance_matrix(
  df = delmarva_counties,
  label_var = "fips",
  lat_var = "latitude",
  long_var = "longitude"
)

## ----eval = TRUE, echo=TRUE---------------------------------------------------
# show the length and first 10 values of the `loc_vec`
length(delmarva_dm$loc_vec)
delmarva_dm$loc_vec |> head(10)

# show the dimension and upper left section of the `distance_matrix`
dim(delmarva_dm$distance_matrix)
delmarva_dm$distance_matrix[1:5, 1:5]

## ----echo=TRUE, eval=TRUE-----------------------------------------------------
maryland_zip_list <- create_dist_list(
  level = "zip",
  # using a small distance for zip code clustering for demo purposes
  threshold = 7,
  st = "MD"
)

# this returns a list
class(maryland_zip_list)

# the list is of length equal to the number of unique zip codes.
# Recall from above that we produced a 621 x 621 matrix; in this
# case, our list is of length 621
length(maryland_zip_list)

# the names of the list are the locations
names(maryland_zip_list) |> head(10)

# each element of the list is a named vector with distances to
# those locations within threshold units
maryland_zip_list |> tail(3)

## ----echo=TRUE, eval=TRUE-----------------------------------------------------
# As before, we use the built-in-counties dataset to get a dataframe of counties
states <- c("Delaware", "Maryland", "Virginia")
delmarva_counties <- counties[state_name %in% states]

head(delmarva_counties, 3)

# Use the custom function to get the distance list
delmarva_dl <- create_custom_dist_list(
  df = delmarva_counties,
  label_var = "fips",
  lat_var = "latitude",
  long_var = "longitude",
  threshold = 50
)

# this is a list
class(delmarva_dl)

# with length equal to all the counties in the Delmarva region
length(delmarva_dl)

# first three elements (i.e. locations) in this list
delmarva_dl[1:3]

Try the gsClusterDetect package in your browser

Any scripts or data that you put into this service are public.

gsClusterDetect documentation built on July 27, 2026, 5:09 p.m.