Skip to contents

Add county name, FIPs, and geometry to data frame with zip codes

Usage

add_counties(
  df,
  statecol,
  zipcol,
  add_fips = FALSE,
  add_geo = FALSE,
  as_sf = FALSE
)

Arguments

df

data frame

statecol

bare column name column containing state abbreviations

zipcol

bare column name containing zip codes

add_fips

add county FIPS code column, default is FALSE

add_geo

add county geometry column, default is FALSE

as_sf

convert tibble to an {sf} object, default is FALSE

Examples

if (FALSE) { # interactive()

# Example data frame containing state abbreviation and zip code
ex <- dplyr::tibble(state = "GA",
                    zip = "31605")
ex

# Adds county name and latitude/longitude
ex |> add_counties(state, zip)

# Adds county FIPS
ex |> add_counties(state,
                   zip,
                   add_fips = TRUE)

# Adds county `geometry` column,
# based on county FIPS column
ex |> add_counties(state,
                   zip,
                   add_fips = TRUE,
                   add_geo  = TRUE)

# Converts data frame to an `sf` object
ex |> add_counties(state,
                   zip,
                   add_fips = TRUE,
                   add_geo  = TRUE,
                   as_sf    = TRUE)
}