library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.5
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(estatapi)
## このサービスは、政府統計総合窓口(e-Stat)のAPI機能を使用していますが、サービスの内容は国によって保証されたものではありません。
library(FactoMineR)
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(showtext)
## Loading required package: sysfonts
## Loading required package: showtextdb
showtext_auto(TRUE)
library(keyring)
URL1 <- "http://api.e-stat.go.jp/rest/3.0/app/getStatsData?cdArea=01100%2C01202%2C01203%2C01204%2C01205%2C01206%2C01207%2C01208%2C01209%2C01210%2C01211%2C01212%2C01213%2C01214%2C01215%2C01216%2C01217%2C01218%2C01219%2C01220%2C01221%2C01222%2C01223%2C01224%2C01225%2C01226%2C01227%2C01228%2C01229%2C01230%2C01231%2C01233%2C01234%2C01235%2C01236%2C01303%2C01304%2C01331%2C01332%2C01333%2C01334%2C01337%2C01343%2C01345%2C01346%2C01347%2C01361%2C01362%2C01363%2C01364%2C01367%2C01370%2C01371%2C01391%2C01392%2C01393%2C01394%2C01395%2C01396%2C01397%2C01398%2C01399%2C01400%2C01401%2C01402%2C01403%2C01404%2C01405%2C01406%2C01407%2C01408%2C01409%2C01423%2C01424%2C01425%2C01427%2C01428%2C01429%2C01430%2C01431%2C01432%2C01433%2C01434%2C01436%2C01437%2C01438%2C01452%2C01453%2C01454%2C01455%2C01456%2C01457%2C01458%2C01459%2C01460%2C01461%2C01462%2C01463%2C01464%2C01465&cdCat01=C120110%2CC120120&appId=&lang=J&statsDataId=0000020203&metaGetFlg=Y&cntGetFlg=N&explanationGetFlg=Y&annotationGetFlg=Y§ionHeaderFlg=1&replaceSpChars=0"
URL2 <- "http://api.e-stat.go.jp/rest/3.0/app/getStatsData?cdArea=01100%2C01202%2C01203%2C01204%2C01205%2C01206%2C01207%2C01208%2C01209%2C01210%2C01211%2C01212%2C01213%2C01214%2C01215%2C01216%2C01217%2C01218%2C01219%2C01220%2C01221%2C01222%2C01223%2C01224%2C01225%2C01226%2C01227%2C01228%2C01229%2C01230%2C01231%2C01233%2C01234%2C01235%2C01236%2C01303%2C01304%2C01331%2C01332%2C01333%2C01334%2C01337%2C01343%2C01345%2C01346%2C01347%2C01361%2C01362%2C01363%2C01364%2C01367%2C01370%2C01371%2C01391%2C01392%2C01393%2C01394%2C01395%2C01396%2C01397%2C01398%2C01399%2C01400%2C01401%2C01402%2C01403%2C01404%2C01405%2C01406%2C01407%2C01408%2C01409%2C01423%2C01424%2C01425%2C01427%2C01428%2C01429%2C01430%2C01431%2C01432%2C01433%2C01434%2C01436%2C01437%2C01438%2C01452%2C01453%2C01454%2C01455%2C01456%2C01457%2C01458%2C01459%2C01460%2C01461%2C01462%2C01463%2C01464%2C01465&cdCat01=F2201%2CF2211%2CF2221&appId=&lang=J&statsDataId=0000020206&metaGetFlg=Y&cntGetFlg=N&explanationGetFlg=Y&annotationGetFlg=Y§ionHeaderFlg=1&replaceSpChars=0"
appId <- keyring::key_get("e-stat") # e-stat のAPI IDは、登録して自分のものを入れてください。
ID1 <- "0000020203" # C 経済基盤
cdCat01_1 <- c("C120110","C120120") #
ID2 <- "0000020206" # F 労働
cdCat01_2 <- c("F2201","F2211","F2221")#
メタ情報は、データセットごとで同じだと思われるので、C経済とF労働は一方を調べればOKのはず。)
meta_info1 <- estat_getMetaInfo(appId = appId, statsDataId = ID1)
meta_info1 %>% names # メタ情報名を確認。
## [1] "tab" "cat01" "area" "time" ".names"
meta_info1$.names
## # A tibble: 4 × 2
## id name
## <chr> <chr>
## 1 tab 観測値
## 2 cat01 C 経済基盤
## 3 area 地域
## 4 time 調査年
meta_info1$area %>% count(`@level`)
## # A tibble: 2 × 2
## `@level` n
## <chr> <int>
## 1 2 1719
## 2 3 198
meta_info1$area %>% filter(`@level`==3) # 区 198
## # A tibble: 198 × 4
## `@code` `@name` `@level` `@parentCode`
## <chr> <chr> <chr> <chr>
## 1 01101 北海道 札幌市 中央区 3 01100
## 2 01102 北海道 札幌市 北区 3 01100
## 3 01103 北海道 札幌市 東区 3 01100
## 4 01104 北海道 札幌市 白石区 3 01100
## 5 01105 北海道 札幌市 豊平区 3 01100
## 6 01106 北海道 札幌市 南区 3 01100
## 7 01107 北海道 札幌市 西区 3 01100
## 8 01108 北海道 札幌市 厚別区 3 01100
## 9 01109 北海道 札幌市 手稲区 3 01100
## 10 01110 北海道 札幌市 清田区 3 01100
## # … with 188 more rows
meta_info1$area %>% filter(`@level`==2) # 市 1719
## # A tibble: 1,719 × 4
## `@code` `@name` `@level` `@parentCode`
## <chr> <chr> <chr> <chr>
## 1 01100 北海道 札幌市 2 01000
## 2 01202 北海道 函館市 2 01000
## 3 01203 北海道 小樽市 2 01000
## 4 01204 北海道 旭川市 2 01000
## 5 01205 北海道 室蘭市 2 01000
## 6 01206 北海道 釧路市 2 01000
## 7 01207 北海道 帯広市 2 01000
## 8 01208 北海道 北見市 2 01000
## 9 01209 北海道 夕張市 2 01000
## 10 01210 北海道 岩見沢市 2 01000
## # … with 1,709 more rows
meta_info2 <- estat_getMetaInfo(appId = appId, statsDataId = ID2)
meta_info2 %>% names # メタ情報名を確認。
## [1] "tab" "cat01" "area" "time" ".names"
meta_info1$area %>% filter(`@level`==2) %>% select(`@code`) %>% unlist -> Area_list0
Area_list0 %>% length()
matrix(Area_list0,100) -> Area_code_matrix
meta_info1$area %>% count(`@level`) %>% mutate(Total=sum(n))
## # A tibble: 2 × 3
## `@level` n Total
## <chr> <int> <int>
## 1 2 1719 1917
## 2 3 198 1917
meta_info1$area %>% DT::datatable()
meta_info1$area %>% filter(`@code` >="13000") %>% filter(`@code` < "14000")
## # A tibble: 63 × 4
## `@code` `@name` `@level` `@parentCode`
## <chr> <chr> <chr> <chr>
## 1 13100 東京都 特別区部 2 13000
## 2 13101 東京都 千代田区 3 13100
## 3 13102 東京都 中央区 3 13100
## 4 13103 東京都 港区 3 13100
## 5 13104 東京都 新宿区 3 13100
## 6 13105 東京都 文京区 3 13100
## 7 13106 東京都 台東区 3 13100
## 8 13107 東京都 墨田区 3 13100
## 9 13108 東京都 江東区 3 13100
## 10 13109 東京都 品川区 3 13100
## # … with 53 more rows
meta_info1$area %>% filter(`@level`==2) %>% filter(`@code`!="13100") -> Area市_特別区部除く
meta_info1$area %>% filter(`@code` >="13101") %>% filter(`@code` <= "13123") -> Area_特別区
Area_all <- bind_rows(Area市_特別区部除く,Area_特別区)
Area_all %>% select(`@code`) %>% unlist -> Area_list0
Area_list0 %>% length()
## [1] 1741
save(Area_list0,file = "Area_list0.rda")
matrix(Area_list0,100) -> Area_code_matrix
## Warning in matrix(Area_list0, 100): data length [1741] is not a sub-multiple or
## multiple of the number of rows [100]
Area_code_matrix %>% dim
## [1] 100 18
Area_code_matrix[,18] # 1:41が必要
## [1] "47327" "47328" "47329" "47348" "47350" "47353" "47354" "47355" "47356"
## [10] "47357" "47358" "47359" "47360" "47361" "47362" "47375" "47381" "47382"
## [19] "13101" "13102" "13103" "13104" "13105" "13106" "13107" "13108" "13109"
## [28] "13110" "13111" "13112" "13113" "13114" "13115" "13116" "13117" "13118"
## [37] "13119" "13120" "13121" "13122" "13123" "01100" "01202" "01203" "01204"
## [46] "01205" "01206" "01207" "01208" "01209" "01210" "01211" "01212" "01213"
## [55] "01214" "01215" "01216" "01217" "01218" "01219" "01220" "01221" "01222"
## [64] "01223" "01224" "01225" "01226" "01227" "01228" "01229" "01230" "01231"
## [73] "01233" "01234" "01235" "01236" "01303" "01304" "01331" "01332" "01333"
## [82] "01334" "01337" "01343" "01345" "01346" "01347" "01361" "01362" "01363"
## [91] "01364" "01367" "01370" "01371" "01391" "01392" "01393" "01394" "01395"
## [100] "01396"
Area_code_matrix[,1]
## [1] "01100" "01202" "01203" "01204" "01205" "01206" "01207" "01208" "01209"
## [10] "01210" "01211" "01212" "01213" "01214" "01215" "01216" "01217" "01218"
## [19] "01219" "01220" "01221" "01222" "01223" "01224" "01225" "01226" "01227"
## [28] "01228" "01229" "01230" "01231" "01233" "01234" "01235" "01236" "01303"
## [37] "01304" "01331" "01332" "01333" "01334" "01337" "01343" "01345" "01346"
## [46] "01347" "01361" "01362" "01363" "01364" "01367" "01370" "01371" "01391"
## [55] "01392" "01393" "01394" "01395" "01396" "01397" "01398" "01399" "01400"
## [64] "01401" "01402" "01403" "01404" "01405" "01406" "01407" "01408" "01409"
## [73] "01423" "01424" "01425" "01427" "01428" "01429" "01430" "01431" "01432"
## [82] "01433" "01434" "01436" "01437" "01438" "01452" "01453" "01454" "01455"
## [91] "01456" "01457" "01458" "01459" "01460" "01461" "01462" "01463" "01464"
## [100] "01465"
meta_info1 %>% names
## [1] "tab" "cat01" "area" "time" ".names"
meta_info1$time %>% count(`@name`)
## # A tibble: 41 × 2
## `@name` n
## <chr> <int>
## 1 1980年度 1
## 2 1981年度 1
## 3 1982年度 1
## 4 1983年度 1
## 5 1984年度 1
## 6 1985年度 1
## 7 1986年度 1
## 8 1987年度 1
## 9 1988年度 1
## 10 1989年度 1
## # … with 31 more rows
# time = 2015年度を使う ...うまく指定できず!
stat_data <- list(NULL)
#Time = "2015年度"
for(i in 1:17){
stat_data[[i]] <- estat_getStatsData(appId=appId,
statsDataId=ID1,
cdCat01=cdCat01_1,
#Time = Time,
cdArea=Area_code_matrix[,i])
}
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
##
## Fetching record 1-7200... (total: 7200 records)
stat_data_all <- NULL
for(i in 1:17){
stat_data_all <- bind_rows(stat_data_all,stat_data[[i]])
}
stat_data_allC <- bind_rows(stat_data_all,
stat_data[[18]] <- estat_getStatsData(appId=appId,
statsDataId=ID1,
cdCat01=cdCat01_1,
cdArea=Area_code_matrix[1:41,18])
)
## Fetching record 1-2952... (total: 2952 records)
stat_data_allC %>% filter(調査年=="2015年度") %>% count(地域)
## # A tibble: 1,741 × 2
## 地域 n
## <chr> <int>
## 1 三重県 いなべ市 2
## 2 三重県 亀山市 2
## 3 三重県 伊勢市 2
## 4 三重県 伊賀市 2
## 5 三重県 南伊勢町 2
## 6 三重県 名張市 2
## 7 三重県 四日市市 2
## 8 三重県 多気町 2
## 9 三重県 大台町 2
## 10 三重県 大紀町 2
## # … with 1,731 more rows
stat_data_allC %>% rename(経済基盤=4) %>% select(経済基盤,地域,調査年,value) %>% count(経済基盤)
## # A tibble: 2 × 2
## 経済基盤 n
## <chr> <int>
## 1 C120110_課税対象所得 62676
## 2 C120120_納税義務者数(所得割) 62676
stat_data_allC %>% rename(経済基盤=4) %>% select(経済基盤,地域,調査年,value) %>% rename(項目=1) %>% filter(調査年=="2015年度")-> 経済基盤
nrow(経済基盤)
## [1] 3482
DT::datatable(経済基盤)
経済基盤 %>% count(地域)
## # A tibble: 1,741 × 2
## 地域 n
## <chr> <int>
## 1 三重県 いなべ市 2
## 2 三重県 亀山市 2
## 3 三重県 伊勢市 2
## 4 三重県 伊賀市 2
## 5 三重県 南伊勢町 2
## 6 三重県 名張市 2
## 7 三重県 四日市市 2
## 8 三重県 多気町 2
## 9 三重県 大台町 2
## 10 三重県 大紀町 2
## # … with 1,731 more rows
tail(経済基盤)
## # A tibble: 6 × 4
## 項目 地域 調査年 value
## <chr> <chr> <chr> <dbl>
## 1 C120120_納税義務者数(所得割) 沖縄県 伊是名村 2015年度 462
## 2 C120120_納税義務者数(所得割) 沖縄県 久米島町 2015年度 2434
## 3 C120120_納税義務者数(所得割) 沖縄県 八重瀬町 2015年度 9861
## 4 C120120_納税義務者数(所得割) 沖縄県 多良間村 2015年度 273
## 5 C120120_納税義務者数(所得割) 沖縄県 竹富町 2015年度 1370
## 6 C120120_納税義務者数(所得割) 沖縄県 与那国町 2015年度 485
stat_dataF <- list(NULL)
for(i in 1:17){
stat_dataF[[i]] <- estat_getStatsData(appId=appId,
statsDataId=ID2,
cdCat01=cdCat01_2,
cdArea=Area_code_matrix[,i])
}
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
##
## Fetching record 1-2400... (total: 2400 records)
stat_data_allF <- NULL
for(i in 1:17){
stat_data_allF <- bind_rows(stat_data_allF,stat_dataF[[i]])
}
stat_data_allF <- bind_rows(stat_data_allF,
stat_dataF[[18]] <- estat_getStatsData(appId=appId,
statsDataId=ID2,
cdCat01=cdCat01_2,
cdArea=Area_code_matrix[1:41,18])
)
## Fetching record 1-984... (total: 984 records)
stat_data_allF
## # A tibble: 41,784 × 11
## tab_code 観測値 cat01_code F 労…¹ area_…² 地域 time_…³ 調査年 unit value
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl>
## 1 00001 観測値 F2201 F2201_第… 01100 北海… 198010… 1980… 人 7398
## 2 00001 観測値 F2201 F2201_第… 01100 北海… 198510… 1985… 人 6591
## 3 00001 観測値 F2201 F2201_第… 01100 北海… 199010… 1990… 人 5116
## 4 00001 観測値 F2201 F2201_第… 01100 北海… 199510… 1995… 人 4526
## 5 00001 観測値 F2201 F2201_第… 01100 北海… 200010… 2000… 人 3789
## 6 00001 観測値 F2201 F2201_第… 01100 北海… 200510… 2005… 人 3552
## 7 00001 観測値 F2201 F2201_第… 01100 北海… 201010… 2010… 人 3534
## 8 00001 観測値 F2201 F2201_第… 01100 北海… 201510… 2015… 人 3790
## 9 00001 観測値 F2201 F2201_第… 01202 北海… 198010… 1980… 人 13570
## 10 00001 観測値 F2201 F2201_第… 01202 北海… 198510… 1985… 人 10749
## # … with 41,774 more rows, 1 more variable: annotation <chr>, and abbreviated
## # variable names ¹`F 労働`, ²area_code, ³time_code
stat_data_allF %>% select(地域) %>% unlist %>% str_detect(pattern = "東京") -> pos_東京
stat_data_allF[pos_東京,] %>% select(地域)
stat_data_allF %>% rename(労働=4) %>% select(労働,地域,調査年,value) %>% count(労働)
## # A tibble: 3 × 2
## 労働 n
## <chr> <int>
## 1 F2201_第1次産業就業者数 13928
## 2 F2211_第2次産業就業者数 13928
## 3 F2221_第3次産業就業者数 13928
stat_data_allF %>% rename(労働=4) %>% select(労働,地域,調査年,value) %>% rename(項目=1) %>% filter(調査年=="2015年度") -> 労働
労働 %>% count(地域)
## # A tibble: 1,741 × 2
## 地域 n
## <chr> <int>
## 1 三重県 いなべ市 3
## 2 三重県 亀山市 3
## 3 三重県 伊勢市 3
## 4 三重県 伊賀市 3
## 5 三重県 南伊勢町 3
## 6 三重県 名張市 3
## 7 三重県 四日市市 3
## 8 三重県 多気町 3
## 9 三重県 大台町 3
## 10 三重県 大紀町 3
## # … with 1,731 more rows
bind_rows(経済基盤 ,労働) %>% filter(調査年=="2015年度") %>%
pivot_wider(id_cols = 地域,names_from = 項目,values_from = value) -> bindCandF
bindCandF %>% names
## [1] "地域" "C120110_課税対象所得"
## [3] "C120120_納税義務者数(所得割)" "F2201_第1次産業就業者数"
## [5] "F2211_第2次産業就業者数" "F2221_第3次産業就業者数"
bindCandF %>% count(地域)
## # A tibble: 1,741 × 2
## 地域 n
## <chr> <int>
## 1 三重県 いなべ市 1
## 2 三重県 亀山市 1
## 3 三重県 伊勢市 1
## 4 三重県 伊賀市 1
## 5 三重県 南伊勢町 1
## 6 三重県 名張市 1
## 7 三重県 四日市市 1
## 8 三重県 多気町 1
## 9 三重県 大台町 1
## 10 三重県 大紀町 1
## # … with 1,731 more rows
bindCandF %>% DT::datatable()
bindCandF %>%
mutate(全産業従事者=F2201_第1次産業就業者数 + F2211_第2次産業就業者数 + F2221_第3次産業就業者数) %>%
mutate(1次産業割合=100*F2201_第1次産業就業者数/全産業従事者) %>%
mutate(2次産業割合=100*F2211_第2次産業就業者数/全産業従事者) %>%
mutate(3次産業割合=100*F2221_第3次産業就業者数/全産業従事者) %>%
mutate(一人あたり課税対象所得=1000*C120110_課税対象所得/`C120120_納税義務者数(所得割)`) -> 集計データ
集計データ %>% names
## [1] "地域" "C120110_課税対象所得"
## [3] "C120120_納税義務者数(所得割)" "F2201_第1次産業就業者数"
## [5] "F2211_第2次産業就業者数" "F2221_第3次産業就業者数"
## [7] "全産業従事者" "1次産業割合"
## [9] "2次産業割合" "3次産業割合"
## [11] "一人あたり課税対象所得"
集計データ %>% count(地域)
## # A tibble: 1,741 × 2
## 地域 n
## <chr> <int>
## 1 三重県 いなべ市 1
## 2 三重県 亀山市 1
## 3 三重県 伊勢市 1
## 4 三重県 伊賀市 1
## 5 三重県 南伊勢町 1
## 6 三重県 名張市 1
## 7 三重県 四日市市 1
## 8 三重県 多気町 1
## 9 三重県 大台町 1
## 10 三重県 大紀町 1
## # … with 1,731 more rows
#集計データ %>% head
#集計データ %>% tail(20)
集計データ %>% select(地域) %>% unlist -> 地域API
length(地域API)# 1741
## [1] 1741
#length(地域Excel)# 1741
集計データ %>% names
## [1] "地域" "C120110_課税対象所得"
## [3] "C120120_納税義務者数(所得割)" "F2201_第1次産業就業者数"
## [5] "F2211_第2次産業就業者数" "F2221_第3次産業就業者数"
## [7] "全産業従事者" "1次産業割合"
## [9] "2次産業割合" "3次産業割合"
## [11] "一人あたり課税対象所得"
write_excel_csv(集計データ,file = "CandFbyAPI.csv") # Excelでの計算確認用
#集計データ %>% select(地域) %>% unlist() %>% str_detect(pattern="福島") -> Area_福島
#集計データ[Area_福島,]
集計データ %>% mutate(1次産業割合 = ifelse(is.nan(1次産業割合), 0, 1次産業割合),
2次産業割合 = ifelse(is.nan(2次産業割合), 0, 2次産業割合),
3次産業割合 = ifelse(is.nan(3次産業割合), 0, 3次産業割合)) -> 集計データ2
集計データ2 %>% count(地域)
## # A tibble: 1,741 × 2
## 地域 n
## <chr> <int>
## 1 三重県 いなべ市 1
## 2 三重県 亀山市 1
## 3 三重県 伊勢市 1
## 4 三重県 伊賀市 1
## 5 三重県 南伊勢町 1
## 6 三重県 名張市 1
## 7 三重県 四日市市 1
## 8 三重県 多気町 1
## 9 三重県 大台町 1
## 10 三重県 大紀町 1
## # … with 1,731 more rows
save(集計データ,file="集計データ.rda") # NaN をそのまま
save(集計データ2,file="集計データ2.rda")# NaNをゼロに置換