Skip to content

Instantly share code, notes, and snippets.

@uribo
Last active January 20, 2020 01:22
Show Gist options
  • Select an option

  • Save uribo/16daa6f72f908bdfb64d70a852487077 to your computer and use it in GitHub Desktop.

Select an option

Save uribo/16daa6f72f908bdfb64d70a852487077 to your computer and use it in GitHub Desktop.
tidyestat series
dl_stat_data_mesharea <- function(tbl_code, meshcode, exdir = ".", dest = TRUE) {
x <-
glue::glue("https://www.e-stat.go.jp/gis/statmap-search/data?statsId={tbl_code}&code={meshcode}&downloadType=2")
destfile <-
glue::glue(
"{exdir}/tbl{tbl_code}H{meshcode}.zip")
utils::download.file(url = x,
destfile = destfile)
if (dest == TRUE)
utils::unzip(
zipfile = destfile,
exdir = exdir
)
}
list_mesh_tbl_code <-
c(`1km` = "T000846",
`500m` = "T000847",
`250m` = "T000876")
# # 左のメニューバー「メッシュコードで絞り込み」をクリックした時のポップアップをコピー
# # {datapasta} のRStudioアドイン paste as vector で貼り付け
# x <-
# c("M3622", "M3623", "M3624", "M3653", "M3724", "M3725", "M3741", "M3831", "M3926", "M3927", "M3928", "M3942", "M4027", "M4028", "M4042", "M4128", "M4129", "M4229", "M4230", "M4329", "M4429", "M4530", "M4531", "M4629", "M4630", "M4631", "M4729", "M4730", "M4731", "M4828", "M4829", "M4830", "M4831", "M4839", "M4928", "M4929", "M4930", "M4931", "M4932", "M4933", "M4934", "M4939", "M5029", "M5030", "M5031", "M5032", "M5033", "M5034", "M5035", "M5036", "M5039", "M5129", "M5130", "M5131", "M5132", "M5133", "M5134", "M5135", "M5136", "M5137", "M5138", "M5139", "M5229", "M5231", "M5232", "M5233", "M5234", "M5235", "M5236", "M5237", "M5238", "M5239", "M5240", "M5332", "M5333", "M5334", "M5335", "M5336", "M5337", "M5338", "M5339", "M5340", "M5432", "M5433", "M5435", "M5436", "M5437", "M5438", "M5439", "M5440", "M5536", "M5537", "M5538", "M5539", "M5540", "M5541", "M5636", "M5637", "M5638", "M5639", "M5640", "M5641", "M5738", "M5739", "M5740", "M5741", "M5839", "M5840", "M5841", "M5939", "M5940", "M5941", "M5942", "M6039", "M6040", "M6041", "M6139", "M6140", "M6141", "M6239", "M6240", "M6241", "M6243", "M6339", "M6340", "M6341", "M6342", "M6343", "M6439", "M6440", "M6441", "M6442", "M6443", "M6444", "M6445", "M6540", "M6541", "M6542", "M6543", "M6544", "M6545", "M6641", "M6642", "M6643", "M6644", "M6645", "M6741", "M6742", "M6840", "M6841", "M6842")
#
# available_stats_meshcode80km <-
# sort(stringr::str_remove(x, "M"))
#
# dput(available_stats_meshcode80km)
available_stats_meshcode80km <-
c("3622", "3623", "3624", "3653", "3724", "3725", "3741", "3831",
"3926", "3927", "3928", "3942", "4027", "4028", "4042", "4128",
"4129", "4229", "4230", "4329", "4429", "4530", "4531", "4629",
"4630", "4631", "4729", "4730", "4731", "4828", "4829", "4830",
"4831", "4839", "4928", "4929", "4930", "4931", "4932", "4933",
"4934", "4939", "5029", "5030", "5031", "5032", "5033", "5034",
"5035", "5036", "5039", "5129", "5130", "5131", "5132", "5133",
"5134", "5135", "5136", "5137", "5138", "5139", "5229", "5231",
"5232", "5233", "5234", "5235", "5236", "5237", "5238", "5239",
"5240", "5332", "5333", "5334", "5335", "5336", "5337", "5338",
"5339", "5340", "5432", "5433", "5435", "5436", "5437", "5438",
"5439", "5440", "5536", "5537", "5538", "5539", "5540", "5541",
"5636", "5637", "5638", "5639", "5640", "5641", "5738", "5739",
"5740", "5741", "5839", "5840", "5841", "5939", "5940", "5941",
"5942", "6039", "6040", "6041", "6139", "6140", "6141", "6239",
"6240", "6241", "6243", "6339", "6340", "6341", "6342", "6343",
"6439", "6440", "6441", "6442", "6443", "6444", "6445", "6540",
"6541", "6542", "6543", "6544", "6545", "6641", "6642", "6643",
"6644", "6645", "6741", "6742", "6840", "6841", "6842")
read_estat_population <- function(file) {
xx <-
as.list(read.csv(
file,
fileEncoding = "cp932",
nrows = 2,
stringsAsFactors = FALSE
)[1,]) %>%
purrr::modify(
~ stringr::str_trim(.x, side = "left") %>%
stringr::str_replace("[[:space:]]", "_") %>%
stringi::stri_trans_nfkc()
)
read.csv(file, fileEncoding = "cp932", stringsAsFactors = FALSE) %>%
tibble::as_tibble() %>%
dplyr::slice(-1) %>%
purrr::set_names(c(names(xx[1:4]),
purrr::flatten_chr(xx[5:length(xx)])))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment