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

# Example data frame containing
# state abbreviation and zip code

(ex <- dplyr::tibble(state = "GA", zip = "31605"))
#> # A tibble: 1 × 2
#>   state zip  
#>   <chr> <chr>
#> 1 GA    31605

# Add county and latitude/longitude
ex |> add_counties(state, zip)
#> # A tibble: 1 × 5
#>   state zip   county    lat   lng
#>   <chr> <chr> <chr>   <dbl> <dbl>
#> 1 GA    31605 Lowndes  31.0 -83.2

# Add county FIPS
ex |> add_counties(state, zip, add_fips = TRUE)
#> # A tibble: 1 × 6
#>   state zip   county    lat   lng county_fips
#>   <chr> <chr> <chr>   <dbl> <dbl> <chr>      
#> 1 GA    31605 Lowndes  31.0 -83.2 13185      

# Add county `geometry` column,
# based on county FIPS column
ex |> add_counties(state, zip, add_fips = TRUE, add_geo = TRUE)
#> # A tibble: 1 × 7
#>   state zip   county    lat   lng county_fips geometry         
#>   <chr> <chr> <chr>   <dbl> <dbl> <chr>       <list>           
#> 1 GA    31605 Lowndes  31.0 -83.2 13185       <MULTIPOLYGON...>

# Convert to an `sf` object
ex |> add_counties(state, zip, add_fips = TRUE, add_geo = TRUE, as_sf = TRUE)
#> Simple feature collection with 1 feature and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -83.23 ymin: 30.95 xmax: -83.23 ymax: 30.95
#> Geodetic CRS:  WGS 84
#> # A tibble: 1 × 5
#>   state zip   county  county_fips       geometry
#> * <chr> <chr> <chr>   <chr>          <POINT [°]>
#> 1 GA    31605 Lowndes 13185       (-83.23 30.95)