Last active
September 28, 2022 04:32
-
-
Save uribo/93ca243eb8b2e58b02bd5ac47ac2f02a to your computer and use it in GitHub Desktop.
郵便番号から該当する都道府県・市区町村・町域名、jis codeを検索
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
read_zipcode_oogaki <- | |
memoise::memoise( | |
function(.path) { | |
dplyr::select(zipangu::read_zipcode(.path, | |
type = "oogaki"), | |
jis_code, zip_code, prefecture, city, street) | |
} | |
) | |
zip_to_administration <- function(x, value = "jis_code", .path = NULL) { | |
if (is.null(.path)) { | |
.path <- | |
"https://www.post.japanpost.jp/zipcode/dl/oogaki/zip/ken_all.zip" | |
} | |
unique( | |
dplyr::pull( | |
dplyr::filter( | |
read_zipcode_oogaki(.path), | |
zip_code == {{ x }}), | |
{{ value }}) | |
) | |
} | |
to_jis_code <- function(x) { | |
df <- | |
dplyr::select(zipangu::jpnprefs, jis_code, prefecture = prefecture_kanji) | |
purrr::map_chr( | |
x, | |
function(x) { | |
if (stringr::str_detect(x, "[0-9]{2}|[0-9]{5}")) { | |
dplyr::pull( | |
dplyr::filter(df, | |
jis_code == stringr::str_sub({{ x }}, 1, 2)), | |
jis_code) | |
} else { | |
dplyr::pull( | |
dplyr::filter(df, | |
prefecture == {{ x }}), | |
jis_code) | |
} | |
} | |
) | |
} |
Author
uribo
commented
Sep 28, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment