Skip to content

Commit 7a048b9

Browse files
* added add_counties
1 parent 3cc904d commit 7a048b9

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

DESCRIPTION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Suggests:
3434
fixtuRes,
3535
fontawesome,
3636
forcats,
37+
zipcodeR,
38+
fipio,
39+
sf,
3740
fs,
3841
ggplot2,
3942
graphics,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export("%nin%")
44
export("%nn%")
55
export("%or%")
6+
export(add_counties)
67
export(age_days)
78
export(arrows)
89
export(as_character)

R/add_counties.R

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#' Add county, FIPS, and geometry to data frame with zip codes
2+
#'
3+
#' @param df data frame
4+
#'
5+
#' @param state bare column name column containing state abbreviations
6+
#'
7+
#' @param zip bare column name containing zip codes
8+
#'
9+
#' @param fips add county FIPS code column, default is `FALSE`
10+
#'
11+
#' @param geo add county geometry column, default is `FALSE`
12+
#'
13+
#' @param sf convert to an `sf` object, default is `FALSE`
14+
#'
15+
#' @examples
16+
#' (ex <- dplyr::tibble(state = "GA", zip = "31605"))
17+
#'
18+
#' # Add county and latitude/longitude
19+
#' ex |> add_counties(state, zip)
20+
#'
21+
#' # Add county FIPS
22+
#' ex |> add_counties(state, zip, fips = TRUE)
23+
#'
24+
#' # Add county `geometry` column,
25+
#' # based on county FIPS column
26+
#' ex |> add_counties(state, zip, fips = TRUE, geo = TRUE)
27+
#'
28+
#' # Convert to an `sf` object
29+
#' ex |> add_counties(state, zip, fips = TRUE, geo = TRUE, sf = TRUE)
30+
#'
31+
#' @autoglobal
32+
#'
33+
#' @export
34+
add_counties <- function(df, state, zip, fips = FALSE, geo = FALSE, sf = FALSE) {
35+
36+
by <- dplyr::join_by(
37+
{{ zip }} == zip,
38+
{{ state }} == state)
39+
40+
df <- dplyr::mutate(
41+
df,
42+
"{{ zip }}" := zipcodeR::normalize_zip({{ zip }}))
43+
44+
zdb <- dplyr::tibble(
45+
zip = zipcodeR::zip_code_db$zipcode,
46+
county = gsub(
47+
" County",
48+
"",
49+
zipcodeR::zip_code_db$county,
50+
fixed = TRUE),
51+
state = zipcodeR::zip_code_db$state,
52+
lat = zipcodeR::zip_code_db$lat,
53+
lng = zipcodeR::zip_code_db$lng)
54+
55+
df <- dplyr::left_join(df, zdb, by)
56+
57+
if (fips) {
58+
59+
pfips <- purrr::possibly(
60+
fipio::coords_to_fips,
61+
otherwise = NA_character_)
62+
63+
df <- dplyr::mutate(
64+
df,
65+
county_fips = purrr::map2(
66+
df$lng,
67+
df$lat,
68+
pfips))
69+
70+
df[apply(
71+
df,
72+
2,
73+
function(x)
74+
lapply(
75+
x,
76+
length
77+
) == 0)
78+
] <- NA
79+
80+
df <- tidyr::unnest(
81+
df,
82+
county_fips,
83+
keep_empty = TRUE)
84+
}
85+
86+
if (geo) {
87+
88+
pgeo <- purrr::possibly(
89+
fipio::fips_geometry,
90+
otherwise = NA_character_)
91+
92+
df <- dplyr::mutate(
93+
df,
94+
geometry = purrr::map(
95+
df$county_fips,
96+
pgeo))
97+
}
98+
99+
if (sf) {
100+
101+
df <- sf::st_as_sf(
102+
df,
103+
coords = c("lng", "lat"),
104+
crs = sf::st_crs(4326),
105+
na.fail = FALSE)
106+
107+
}
108+
return(df)
109+
}
110+
111+
# dplyr::tibble(
112+
# city = c('Mount Zion', 'Montezuma', 'Thomaston'),
113+
# state = fct_stabb(rep('GA', 3)),
114+
# county = c('Carroll', 'Macon', 'Upson'),
115+
# zip = c('30150', '31063', '30286'),
116+
# county_fips = c('13045', '13193', '13293')
117+
# )

R/generated-globals.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxyglobals: do not edit by hand
22

33
utils::globalVariables(c(
4+
# <add_counties>
45
# <age_days>
56
# <count_days>
67
# <sorted_bars>
@@ -12,6 +13,8 @@ utils::globalVariables(c(
1213
"combined",
1314
# <rate_of_return>
1415
"copy",
16+
# <add_counties>
17+
"county_fips",
1518
# <forager_data>
1619
"date_of_acceptance",
1720
# <forager_data>

man/add_counties.Rd

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)