Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made some changes to ojo_parse_county and added ojo_list_counties #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
512 changes: 512 additions & 0 deletions .Rhistory

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

export(limit)
export(ojo_counties)
export(ojo_list_counties)
export(ojo_oscn_counties)
export(ojo_parse_county)
importFrom(dplyr,if_else)
importFrom(stringr,str_remove_all)
Expand Down
72 changes: 63 additions & 9 deletions R/county.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@ ojo_counties <- c(
"tillman", "tulsa", "wagoner", "washington", "washita", "woods", "woodward"
)

#' @title Oklahoma OSCN reporting counties
#'
#' @export ojo_oscn_counties
#'
ojo_oscn_counties <- c(
"adair", "canadian", "cleveland", "comanche",
"ellis", "garfield", "logan", "oklahoma", "payne",
"pushmataha", "rogermills", "rogers", "tulsa"
)

#' @title Parse Oklahoma Counties
#'
#' @importFrom stringr str_remove_all str_to_lower
#' @importFrom dplyr if_else
#'
#' @export ojo_parse_county
#'
ojo_parse_county <- function(county, ..., case = "lower", squish = NULL, suffix = NULL, counties = ojo_counties, .silent = FALSE) {
ojo_parse_county <- function(county, ..., case = "lower", squish = FALSE, suffix = NULL,
counties = ojo_counties, .silent = FALSE) {

# Check case arg
case <- rlang::arg_match0(
arg = stringr::str_to_lower(case),
values = c("lower", "upper", "title")
Expand All @@ -42,21 +55,32 @@ ojo_parse_county <- function(county, ..., case = "lower", squish = NULL, suffix

# Apply Casing
county <- dplyr::case_when(
# Adjusting for unusual district names
tolower(county) == "rogermills" & case == "lower" ~ "roger mills",
tolower(county) == "rogermills" & case == "title" ~ "Roger Mills",
tolower(county) == "rogermills" & case == "upper" ~ "ROGER MILLS",
tolower(county) == "leflore" & case == "title" ~ "LeFlore",
tolower(county) == "mcclain" & case == "title" ~ "McClain",
tolower(county) == "mccurtain" & case == "title" ~ "McCurtain",
# Normal behavior
case == "lower" ~ stringr::str_to_lower(county),
case == "upper" ~ stringr::str_to_upper(county),
case == "title" ~ stringr::str_to_title(county)
case == "title" ~ stringr::str_to_title(county),
)

if (case == "title") {
county <- dplyr::case_when(
county == "Leflore" ~ "LeFlore",
county == "Mcclain" ~ "McClain",
county == "Mccurtain" ~ "McCurtain",
county == "Rogermills" ~ "Roger Mills",
TRUE ~ county
# Add suffix if requested
if (!is.null(suffix)) {
# Change case...
suffix <- dplyr::case_when(
case == "lower" ~ stringr::str_to_lower(suffix),
case == "upper" ~ stringr::str_to_upper(suffix),
case == "title" ~ stringr::str_to_title(suffix),
)
# ...then add
county <- stringr::str_c(county, suffix, sep = " ")
}

# Remove all spaces if requested
if (isTRUE(squish)) {
county <- stringr::str_remove_all(county, "\\s")
}
Expand All @@ -66,3 +90,33 @@ ojo_parse_county <- function(county, ..., case = "lower", squish = NULL, suffix

return(county)
}

#' @title List Oklahoma Counties
#'
#' @export ojo_list_counties
#'
ojo_list_counties <- function(case = "lower", squish = FALSE, suffix = NULL,
oscn_only = FALSE){

# Clean the list of counties using ojo_parse_counties() and then return
county <- ojo_counties |>
dplyr::as_tibble() |>
dplyr::mutate(
value = ojoutils::ojo_parse_county(county = value,
case = case,
squish = squish,
suffix = suffix,
.silent = TRUE)
)

# Only return OSCN counties?
if (oscn_only) {
county <- county |>
dplyr::filter(
ojo_parse_county(value, case = "lower", squish = TRUE) %in% ojo_oscn_counties
)
}

return(county)

}
Loading