Skip to content

Commit

Permalink
allow match to take a vector input
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherkenny committed May 30, 2024
1 parent d5cbc63 commit 40cd841
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 46 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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'`.
Expand Down
52 changes: 9 additions & 43 deletions R/match.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)]
}


Expand All @@ -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)
}

0 comments on commit 40cd841

Please sign in to comment.