From 40cd841a4523e477a479c1e292aa90659f95961e Mon Sep 17 00:00:00 2001 From: Christopher Kenny Date: Wed, 29 May 2024 21:56:09 -0400 Subject: [PATCH] allow match to take a vector input --- DESCRIPTION | 5 ++--- NEWS.md | 4 ++++ R/match.R | 52 +++++++++------------------------------------------- 3 files changed, 15 insertions(+), 46 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 04c0aef..40136bd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,13 +1,12 @@ Package: censable Title: Making Census Data More Usable -Version: 0.0.6 +Version: 0.0.7 Authors@R: person(given = "Christopher T.", family = "Kenny", role = c("aut", "cre"), email = "christopherkenny@fas.harvard.edu", comment = c(ORCID = "0000-0002-9386-6860")) -Date: 2022-11-19 URL: https://christophertkenny.com/censable/, https://github.com/christopherkenny/censable BugReports: https://github.com/christopherkenny/censable/issues Description: Creates a common framework for organizing, naming, and gathering @@ -18,7 +17,7 @@ License: MIT + file LICENSE Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Suggests: roxygen2, spelling, diff --git a/NEWS.md b/NEWS.md index faa895f..58aa103 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# censable 0.0.7 + +* Add support for `match_*()` function to take multiple states at once. + # censable 0.0.6 * Fixes an error where `build_dec()` and `build_acs()` fail when `geography = 'state'`. diff --git a/R/match.R b/R/match.R index 62d5deb..8a06df9 100644 --- a/R/match.R +++ b/R/match.R @@ -12,18 +12,7 @@ #' match_fips('NY') #' match_fips('01') match_fips <- function(state) { - stata <- censable::stata - pos <- tolower(c(stata$fips, stata$abb, stata$name, stata$ansi)) - state <- tolower(state) - matched <- which(state == pos) - - if (length(matched) == 0) { - matched <- agrep(pattern = state, x = pos) - } - - matched <- (matched %% nrow(stata)) - matched <- ifelse(matched == 0, 57, matched) - stata$fips[matched] + censable::stata$fips[get_state_matches(state)] } #' Try to Match to State Abbreviation @@ -40,18 +29,7 @@ match_fips <- function(state) { #' match_abb('NY') #' match_abb('01') match_abb <- function(state) { - stata <- censable::stata - pos <- tolower(c(stata$fips, stata$abb, stata$name, stata$ansi)) - state <- tolower(state) - matched <- which(state == pos) - - if (length(matched) == 0) { - matched <- agrep(pattern = state, x = pos) - } - - matched <- (matched %% nrow(stata)) - matched <- ifelse(matched == 0, 57, matched) - stata$abb[matched] + censable::stata$abb[get_state_matches(state)] } #' Try to Match to State Name @@ -68,18 +46,7 @@ match_abb <- function(state) { #' match_name('NY') #' match_name('01') match_name <- function(state) { - stata <- censable::stata - pos <- tolower(c(stata$fips, stata$abb, stata$name, stata$ansi)) - state <- tolower(state) - matched <- which(state == pos) - - if (length(matched) == 0) { - matched <- agrep(pattern = state, x = pos) - } - - matched <- (matched %% nrow(stata)) - matched <- ifelse(matched == 0, 57, matched) - stata$name[matched] + censable::stata$name[get_state_matches(state)] } @@ -97,16 +64,15 @@ match_name <- function(state) { #' match_ansi('NY') #' match_ansi('01') match_ansi <- function(state) { + censable::stata$ansi[get_state_matches(state)] +} + +get_state_matches <- function(state) { stata <- censable::stata pos <- tolower(c(stata$fips, stata$abb, stata$name, stata$ansi)) state <- tolower(state) - matched <- which(state == pos) - - if (length(matched) == 0) { - matched <- agrep(pattern = state, x = pos) - } + matched <- match(state, pos) matched <- (matched %% nrow(stata)) - matched <- ifelse(matched == 0, 57, matched) - stata$ansi[matched] + ifelse(matched == 0, 57, matched) }