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

Add japan zip-code utility functions #6

Merged
merged 9 commits into from Dec 11, 2019
Merged

Add japan zip-code utility functions #6

merged 9 commits into from Dec 11, 2019

Conversation

uribo
Copy link
Owner

@uribo uribo commented Dec 10, 2019

Summary

郵便番号に関する4つの関数を追加。

  • read_zipcode()... 日本郵便が提供する郵便番号データ(csv形式)を読み込む。コンピュータ上のファイルかURLでの指定でzipファイルを直接読み込む(この処理に dl_zipcode_file() を利用)
  • dl_zipcode_file() ... 郵便番号データのzipファイルをダウンロード・展開する
  • is_zipcode()... 数値・文字列が7桁の郵便番号であるか
  • zipcode_spacer()... 郵便番号文字列に含まれるハイフンを除外または追加
Demo

read_zipcode()

ダウンロード可能な4種類のファイルに対応済み。これらのファイルの種類は引数 type でユーザが指定する。

  • oogaki ... 読み仮名データの促音・拗音を小書きで表記しないもの
  • kogaki ... 読み仮名データの促音・拗音を小書きで表記するもの
  • roman ... ローマ字
  • jigyosyo ... 事業所
read_zipcode("~/Documents/projects2019/jp-address/data-raw/japanpost_oogaki/KEN_ALL.CSV", type = "oogaki")
#> # A tibble: 124,351 x 15
#> jis_code old_zip_code zip_code prefecture_kana city_kana street_kana prefecture city  street is_street_dupli… is_banchi is_cyoumoku
#> <chr>    <chr>        <chr>    <chr>           <chr>     <chr>       <chr>      <chr> <chr>             <dbl>     <dbl>       <dbl>
#> 1 01101    064          0640941  ホツカイドウ    サツポロシチユウ… アサヒガオカ… 北海道     札幌市中… 旭ケ丘                0         0           1
#> 2 01101    060          0600041  ホツカイドウ    サツポロシチユウ… オオドオリヒガシ… 北海道     札幌市中… 大通東                0         0           1
#> 3 01101    060          0600042  ホツカイドウ    サツポロシチユウ… オオドオリニシ(1-… 北海道     札幌市中… 大通西(1…                1         0           1
#> 4 01101    064          0640820  ホツカイドウ    サツポロシチユウ… オオドオリニシ(20… 北海道     札幌市中… 大通西(2…                1         0           1
#> 5 01101    060          0600031  ホツカイドウ    サツポロシチユウ… キタ1ジヨウヒガシ… 北海道     札幌市中… 北一条東…                0         0           1
#> 6 01101    060          0600001  ホツカイドウ    サツポロシチユウ… キタ1ジヨウニシ(1… 北海道     札幌市中… 北一条西(…                1         0           1
#> 7 01101    064          0640821  ホツカイドウ    サツポロシチユウ… キタ1ジヨウニシ(2… 北海道     札幌市中… 北一条西(…                1         0           1
#> 8 01101    060          0600032  ホツカイドウ    サツポロシチユウ… キタ2ジヨウヒガシ… 北海道     札幌市中… 北二条東…                0         0           1
#> 9 01101    060          0600002  ホツカイドウ    サツポロシチユウ… キタ2ジヨウニシ(1… 北海道     札幌市中… 北二条西(…                1         0           1
#>10 01101    064          0640822  ホツカイドウ    サツポロシチユウ… キタ2ジヨウニシ(2… 北海道     札幌市中… 北二条西(…                1         0           1
#># … with 124,341 more rows, and 3 more variables: is_zipcode_duplicate <dbl>, status <dbl>, modify_type <dbl>

is_zipcode()

is_zipcode(7000027)
#> [1] TRUE
is_zipcode("305-0053")
#> [1] TRUE
is_zipcode(700)
#> 7桁の数値ではありません
#> [1] FALSE

zipcode_spacer()

zipcode_spacer("3050053")
#> [1] "305-0053"
zipcode_spacer("700-0027", remove = TRUE)
#> [1] "7000027"

Reviewer Checkpoint

  • is_zipcode() の郵便番号判定をより厳密にする必要があるか
  • read_zipcode() の exampleおよびtest, coverageのためにrawデータを保存するか。具体的には system.file("japanpost/oogaki/13TOKYO.CSV", package = "zipangu") のような形式で呼び出せるようにしておくかどうか。
  • read_zipcode() が返却する列名は妥当か。日本語との対応を確認する必要がある

Related issues

#5 の第一歩として、日本郵便提供の郵便番号データをRで読み込みやすいように。

- 日本郵便が提供する郵便番号データ(csv形式)を読み込む
- 数値・文字列が7桁の郵便番号であるか
- 郵便番号文字列に含まれるハイフンを除外または追加
@uribo uribo marked this pull request as ready for review December 10, 2019 10:16
R/zipcode.R Outdated Show resolved Hide resolved
@uribo uribo added the Topic: address & zipcode 📮 住所、郵便番号 label Dec 10, 2019
@uribo
Copy link
Owner Author

uribo commented Dec 10, 2019

read_zipcode() の設計として、コンピュータ内にあるファイルだけでなく、 https://www.post.japanpost.jp/ ドメインのzipファイルを直接指定できるようにするのはどうでしょう?

read_zipcode("https://www.post.japanpost.jp/zipcode/dl/oogaki/zip/01hokkai.zip", "oogaki") が実行された場合はzipファイルを tempdir()download.file() で保存。 unzip() 後にそのファイルを読み込む。もし 一時フォルダに同じファイルがあれば2回目以降はそこから読み込む。

zipcode_spacer <- function(x, remove = FALSE) {
purrr::map_chr(x,
~ if (rlang::is_true(is_zipcode(.x)))
if (rlang::is_false(remove)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ややこしいので rlang::is_false なしでもよさそう?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

うーむ、こちらは残しておきたいです

R/zipcode.R Outdated Show resolved Hide resolved
@uribo uribo merged commit 94ca902 into master Dec 11, 2019
@uribo uribo deleted the feature/zipcode branch December 11, 2019 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Topic: address & zipcode 📮 住所、郵便番号
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants