データの取得に当たっては、チャート集末尾の出典にあるジャッグジャパン社のサイトを基本に、自治体やエンジニア団体などによるサイト(ただし、後者はCC-BYなどのライセンス表記があるもの)も含めて取得し、都道府県別で累積数が多い方を採用した。なお、自治体・団体等についてはデータの更新時に合わせて一時的に非公開になることもあれば、ジャッグジャパン社データもファイルやエンコードの形式の変更によってうまく読み込めなくなることもあるため(エンコードの関係の陽で、このスクリプトでうまく読み込めない場合は「ダウンロード」フォルダに一旦、保存の上、表計算やメモ帳で開いて再保存してみるとうまくいくようです)例外処理(tryCatch)を用いている。 一部に漏れや不完全なところもありますが、Rでも普通に動くと思いますので、分析等のためのデータ取得などに活用されたい方はご自由に。 【主な変更】 ・5月9~10日 できるだけ一次情報(自治体サイト)から取得するよう、ソースを修正 ・5月12日 ジャッグジャパン社のデータについて、 の関係で読み込めないケースがあることから、httrパッケージでレスポンスの取得から行うコードに修正 ・5月13日 島根のコードに誤りがあった(数名過剰に計上されるようになっていた)ため修正 ・5月14日 東京都の直近日のデータについて、都のサイト繁栄まではジャッグジャパン社のデータを挿入するよう修正 ・5月18日 秋田県が再びtable要素で読めるようになったため修正、香川県のHTML上のテーブルの位置が変わったため修正、熊本県のJSONのデータ形式が変わったため修正 ・6月29日 京都府、鹿児島県、香川県、新潟県、宮崎県についてデータの形式の変更に応じて修正 ・9月16日 諸々を修正 ・11月17日 諸々を修正 ・11月21日 諸々を修正
library(dplyr)
library(tidyr)
library(lubridate)
library(rvest)
library(readr)
library(stringr)
library(jsonlite)
library(openxlsx)
library(pdftools)
#library(data.table)
library(httr)
#library(jqr)
library(data.table)
dummy<- data.frame(matrix(NA,nrow=1,ncol=5))
colnames(dummy)<- c("確定日","居住都道府県","年代","性別","受診都道府県")
dummy[1,2:5]<- c("d","u","m","y")
dummy_jj<- dummy
dummy_jj$確定日[1]<- "04/01/2020"
dummy$確定日[1]<- "2020-04-01"
dummy$確定日<- as.Date(dummy$確定日)
convfunc<- function(x){
str_replace_all(x,"1","1") %>%
str_replace_all("2","2") %>%
str_replace_all("3","3") %>%
str_replace_all("4","4") %>%
str_replace_all("5","5") %>%
str_replace_all("6","6") %>%
str_replace_all("7","7") %>%
str_replace_all("8","8") %>%
str_replace_all("9","9") %>%
str_replace_all("0","0")
}
getdata<- function(df){
#北海道:csvオープンデータ
hokkaido<- tryCatch({
hokkaido<- read.csv(
"https://www.harp.lg.jp/opendata/dataset/1369/resource/2828/patients.csv")[,c(2,4:6)]
colnames(hokkaido)<- c("確定日","居住都道府県","年代","性別")
hokkaido[,1]<- as.Date(hokkaido[,1])
hokkaido<- transform(hokkaido,受診都道府県="北海道")
hokkaido
},error=function(e){
hokkaido<- dummy
hokkaido$受診都道府県<- "北海道"
hokkaido
})
#青森:csvオープンデータ、URL変化あり
aomori<- tryCatch({
#サイトHTMLからURLリンクを取得、一番最後のURLを最新として抽出
aomori_html<- read_html("https://opendata.pref.aomori.lg.jp/dataset/1531.html")
aomori_text<- html_text(aomori_html)
aomori_splt<- str_split(aomori_text,"\n")[[1]]
aomori_splt<- str_remove_all(aomori_splt," ")
aomori_fname<- aomori_splt[which(str_detect(aomori_splt,"陽性患者関係"))]
aomori_fname<- str_replace_all(aomori_fname," ","")
aomori_fname<- strsplit(aomori_fname,".csv")[[1]][1]
aomori_furl<- paste0("https://opendata.pref.aomori.lg.jp/dataset/1531/resource/12379/",
str_replace(aomori_fname,"陽性患者関係","%E9%99%BD%E6%80%A7%E6%82%A3%E8%80%85%E9%96%A2%E4%BF%82"),
".csv")
#以降は通常のcsv取り込み
aomori<- read.csv(aomori_furl)[,c(3,6:9)]
colnames(aomori)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
aomori<- aomori[,c(2:5,1)]
aomori$確定日<- ymd(aomori$確定日)
aomori
},error=function(e){
aomori<- dummy
aomori$受診都道府県[1]<- "青森県"
aomori
})
#秋田:stopcovidのHTMLスクレイピング→県サイトのHTMLスクレイピング
akita<- tryCatch({
#サイトHTMLからテーブルノードを抽出
# akita_html<- read_html(
# "https://covid19-akita.netlify.app/cards/attributes-of-confirmed-cases/")
# akita<- html_table(akita_html)[[1]][,1:4]
#以降はデータ整形
# colnames(akita)<- c("確定日","居住都道府県","年代","性別")
# akita$確定日<- paste0("2020/",akita$確定日)
# akita$確定日<- ymd(akita$確定日)
# akita<- transform(akita,受診都道府県="秋田県")
# akita
#URLからHTMLノード取得
akita_url<- "https://www.pref.akita.lg.jp/pages/archive/47957"
# akita_html<- read_html(akita_url) %>% html_nodes("p")
#p属性中、該当しそうなもの抽出し、陽性者数を判断
# akita_table<- akita_html[str_detect(akita_html,'p style="padding-left: 30px;"')==T]
# akita_text<- html_text(akita_table)
# akita_text<- akita_text[str_detect(akita_text,"年齢")]
# akita_num<- length(akita_text)
#器を作成
# akita_matrix<- data.frame(matrix(NA,ncol=4,nrow=akita_num))
# colnames(akita_matrix)<- c("確定日","居住都道府県","年代","性別")
#月日と紛らわしいので削除
# akita_text<- str_remove_all(akita_text,"日本")
#確定日は月から日の間に2020年を加える
# akita_matrix$確定日<- paste0("2020年",
# str_sub(akita_text,start=regexpr("月",akita_text)-1,
# end=regexpr("月",akita_text)),
# str_sub(akita_text,start=regexpr("月",akita_text)+1,
# end=regexpr("日",akita_text))) %>%
# ymd()
#居住地は「居住地:」から「職業」又は「状況」までの間
# akita_matrix$居住都道府県<- str_sub(akita_text,
# start=regexpr("居住地:",akita_text)+4,
# end=regexpr("(職業|状況)",akita_text)-2)
#年代は「年齢」から「性別」の間
# akita_matrix$年代<- str_sub(akita_text,
# start=regexpr("年齢:",akita_text)+3,
# end=regexpr("性別",akita_text)-2) %>%
# as.factor()
#性別は「性別」から「居住地」又は「国籍」までの間
# akita_matrix$性別<- str_sub(akita_text,
# start=regexpr("性別:",akita_text)+3,
# end=regexpr("(居住地|国籍)",akita_text)-2) %>%
# as.factor()
#table属性で再び取得可能になったためやりなおし
akita_table<- read_html(akita_url) %>%
html_nodes("table") %>%
html_table
akita_table02<- read_html("https://www.pref.akita.lg.jp/pages/archive/53190#gaiyou") %>%
html_nodes("table") %>%
html_table
akita_table<- rbind(akita_table[[1]],akita_table02[[1]])
akita_matrix<- akita_table[,c(2,5,3,4)]
colnames(akita_matrix)<- c("確定日","居住都道府県","年代","性別")
akita_matrix$確定日<- paste0("2020年",akita_matrix$確定日) %>% ymd()
akita_matrix$年代<- as.factor(akita_matrix$年代)
akita_matrix$性別<- as.factor(akita_matrix$性別)
#受診都道府県を追加
akita<- transform(akita_matrix,受診都道府県="秋田県")
akita
},error=function(e){
akita<- dummy
akita$受診都道府県[1]<- "秋田県"
akita
})
#岩手
iwate<- tryCatch({
iwate_url01<- "https://www.pref.iwate.jp/kurashikankyou/iryou/covid19/1029635/index.html"
iwate_url02<- "https://www.pref.iwate.jp/kurashikankyou/iryou/covid19/1034904/index.html"
iwate_table01<- read_html(iwate_url01) %>% html_table()
iwate_table01<- iwate_table01[[1]][,c(5,4,2,3)]
iwate_table02<- read_html(iwate_url02) %>% html_table()
iwate_table02<- iwate_table02[[1]][,c(5,4,2,3)]
iwate_table<- rbind(iwate_table01,iwate_table02)
colnames(iwate_table)<- c("確定日","居住都道府県","年代","性別")
iwate_table$確定日<- gsub("令和2年","2020年",iwate_table$確定日) %>% ymd()
iwate<- transform(iwate_table,受診都道府県="岩手県")
iwate
},error=function(e){
iwate<- dummy
iwate$受診都道府県[1]<- "岩手県"
iwate
})
#宮城:stopcovidのHTMLスクレイピング→xlsxオープンデータ
miyagi<- tryCatch({
# miyagi_html<- read_html(
# "https://miyagi.stopcovid19.jp/cards/attributes-of-confirmed-cases/")
# miyagi<- html_table(miyagi_html)[[1]][,1:4] %>% mutate(公表日 = recode(公表日, "Invalid Date" = "04/14"))
# colnames(miyagi)<- c("確定日","居住都道府県","年代","性別")
# miyagi$確定日<- paste0("2020/",miyagi$確定日)
# miyagi$確定日<- ymd(miyagi$確定日)
# miyagi<- transform(miyagi,受診都道府県="宮城県")
# miyagi
#URL取得
miyagi_url<- "https://www.pref.miyagi.jp/site/covid-19/02.html"
miyagi_html<- read_html(miyagi_url) %>% html_nodes("a") %>% html_attr("href")
miyagi_link<- miyagi_html[str_detect(miyagi_html,".xlsx")==T]
miyagi_link<- paste0("https://www.pref.miyagi.jp/",miyagi_link[is.na(miyagi_link)==F])
#xlsxファイルからの読み込み
miyagi<- read.xlsx(miyagi_link,sheet="患者状況一覧(HP掲載)",startRow=3)[,c(5,4,2,3)]
colnames(miyagi)<- c("確定日","居住都道府県","年代","性別")
miyagi$確定日<- as.Date(miyagi$確定日,origin="1899-12-30")
miyagi$年代<- as.factor(miyagi$年代)
miyagi$性別<- as.factor(miyagi$性別)
miyagi<- transform(miyagi,受診都道府県="宮城県")
miyagi<- miyagi[miyagi$確定日>"2020-01-01",]
miyagi
},error=function(e){
miyagi<- dummy
miyagi$受診都道府県[1]<- "宮城県"
miyagi
})
#山形:stopcovidのHTMLスクレイピング
yamagata<- tryCatch({
yamagata_html<- read_html(
"https://stopcovid19-yamagata.netlify.app/cards/attributes-of-confirmed-cases/")
yamagata<- html_table(yamagata_html)[[1]][,1:4]
colnames(yamagata)<- c("確定日","居住都道府県","年代","性別")
yamagata$確定日<- paste0("2020/",yamagata$確定日)
yamagata$確定日<- ymd(yamagata$確定日)
yamagata<- transform(yamagata,受診都道府県="山形県")
yamagata
},error=function(e){
yamagata<- dummy
yamagata$受診都道府県[1]<- "山形県"
yamagata
})
#福島:csvオープンデータ、URL変化あり
fukushima<- tryCatch({
fukushima_html<- read_html(
"http://www.pref.fukushima.lg.jp/w4/covid19/patients/")
fukushima_link<- html_nodes(fukushima_html,"a") %>% html_attr("href")
fukushima_url<- paste0("http://www.pref.fukushima.lg.jp/w4/covid19/patients/",
fukushima_link[length(fukushima_link)])
fukushima<- read.csv(fukushima_url)[,c(3,5,7:9)]
colnames(fukushima)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
fukushima<- fukushima[,c(2:5,1)]
fukushima$確定日<- ymd(fukushima$確定日)
fukushima
},error=function(e){
fukushima<- dummy
fukushima$受診都道府県[1]<- "福島県"
fukushima
})
#栃木:stopcovidのHTMLスクレイピング→xlsxオープンデータ
tochigi<- tryCatch({
# tochigi_html<- read_html(
# "https://covid19-tochigi.netlify.app/cards/attributes-of-confirmed-cases/")
# tochigi<- html_table(tochigi_html)[[1]][,1:4]
# colnames(tochigi)<- c("確定日","居住都道府県","年代","性別")
# tochigi$確定日<- paste0("2020/",tochigi$確定日)
# tochigi$確定日<- ymd(tochigi$確定日)
# tochigi<- transform(tochigi,受診都道府県="栃木県")
# tochigi
#URL取得
tochigi_url<- "http://www.pref.tochigi.lg.jp/e04/welfare/hoken-eisei/kansen/hp/coronakensahasseijyoukyou.html"
tochigi_html<- read_html(tochigi_url) %>% html_nodes("a") %>% html_attr("href")
tochigi_link<- tochigi_html[str_detect(tochigi_html,"hasseijokyou.xlsx")==T &
!is.na(str_detect(tochigi_html,"hasseijokyou.xlsx"))]
tochigi_link<- paste0("http://www.pref.tochigi.lg.jp",tochigi_link)
#xlsxファイルからの読み込み
tochigi<- read.xlsx(tochigi_link,sheet=1,startRow=2)[,c(6,5,3,4)]
colnames(tochigi)<- c("確定日","居住都道府県","年代","性別")
tochigi<- tochigi[is.na(tochigi$確定日)==F,]
#取り下げのようなので削除
tochigi<- tochigi[-55,]
tochigi$確定日<- as.Date(tochigi$確定日,origin="1899-12-30")
tochigi$年代<- as.factor(tochigi$年代)
tochigi$性別<- as.factor(tochigi$性別)
tochigi<- transform(tochigi,受診都道府県="栃木県")
tochigi
},error=function(e){
tochigi<- dummy
tochigi$受診都道府県[1]<- "栃木県"
tochigi
})
#群馬:stopcovidのHTMLスクレイピング→PDFスクレイピング
gunma<- tryCatch({
# gunma_html<- read_html(
# "http://stopcovid19.pref.gunma.jp/cards/attributes-of-confirmed-cases/")
# gunma<- html_table(gunma_html)[[1]][,1:4]
# colnames(gunma)<- c("確定日","居住都道府県","年代","性別")
# gunma$確定日<- paste0("2020/",gunma$確定日)
# gunma$確定日<- ymd(gunma$確定日)
# gunma<- transform(gunma,受診都道府県="群馬県")
# gunma
#PDFのURLを取得
gunma_url<- "https://www.pref.gunma.jp/07/z87g_00016.html"
gunma_html<- read_html(gunma_url) %>% html_nodes("a") %>% html_attr("href")
gunma_link<- paste0("https://www.pref.gunma.jp",
subset(gunma_html,str_detect(gunma_html,".pdf")))[1]
#PDFのテキスト情報とページ数を抽出
# gunma_text<- pdf_text(gunma_link)
gunma_text<- tryCatch({
gunma_text<- pdf_text(gunma_link)
},error=function(e){
gunma_text<- pdf_text(gunma_link)
},error=function(e){
gunma_text<- pdf_text(gunma_link)
},error=function(e){
gunma_text<- pdf_text(gunma_link)
},error=function(e){
gunma_text<- pdf_text(gunma_link)
})
# gunma_page<- pdf_info(gunma_link)$pages
gunma_page<- tryCatch({
gunma_page<- pdf_info(gunma_link)$pages
},error=function(e){
gunma_page<- pdf_info(gunma_link)$pages
},error=function(e){
gunma_page<- pdf_info(gunma_link)$pages
},error=function(e){
gunma_page<- pdf_info(gunma_link)$pages
},error=function(e){
gunma_page<- pdf_info(gunma_link)$pages
})
gunma_table<- vector()
#ループでスペースで区切り、ベクトルに
for(i in 1:gunma_page){
gunma_table<- c(gunma_table,str_split(gunma_text[i],"\r\n")[[1]])
}
#陽性者情報以外を削除、数字を半角に
gunma_table<- subset(gunma_table,str_detect(gunma_table,"例目")==T &
str_detect(gunma_table,"R")==T)
gunma_table<- convfunc(gunma_table)
#陽性者数を把握、器を作成
gunma_num<- length(gunma_table)
gunma_matrix<- data.frame(matrix(NA,ncol=4,nrow=gunma_num))
colnames(gunma_matrix)<- c("確定日","居住都道府県","年代","性別")
#各行をスペースで区切り、必要な情報を器に合わせて並べて整形
for(i in 1:gunma_num){
temp<- str_split(gunma_table[i]," ")[[1]]
temp<- temp[temp!=""]
temp<- temp[c(3,5,4,6)]
gunma_matrix[i,]<- temp
}
#Before September
gunma_link02<- c("https://www.pref.gunma.jp/contents/100173861.pdf",
"https://www.pref.gunma.jp/contents/100173917.pdf",
"https://www.pref.gunma.jp/contents/100173918.pdf",
"https://www.pref.gunma.jp/contents/100173919.pdf",
"https://www.pref.gunma.jp/contents/100173920.pdf",
"https://www.pref.gunma.jp/contents/100175030.pdf",
"https://www.pref.gunma.jp/contents/100174263.pdf")
temp<- vector()
temp02<- vector()
temp03<- vector()
temp04<- vector()
temppage<- vector()
for(i in gunma_link02){
temp02<- tryCatch({
pdf_text(i)
},error=function(e){
pdf_text(i)
},error=function(e){
pdf_text(i)
},error=function(e){
pdf_text(i)
},error=function(e){
pdf_text(i)
})
temppage<- length(temp02)
for(j in 1:temppage){
if(j==1){
temp04<- strsplit(temp02[j],"\r\n")[[1]]
temp03<- temp04[4:length(temp04)]
}else{
temp04<- strsplit(temp02[j],"\r\n")[[1]]
temp03<- c(temp03,temp04[2:length(temp04)])
}
}
temp03<- temp03[str_detect(temp03,"例目")==T]
temp<- c(temp,temp03)
}
numdata<- length(temp)
temp05<- vector()
gunma_matrix02<- data.frame(matrix(NA,ncol=10,nrow=numdata))
for(i in 1:numdata){
j<- 1
temp05<- strsplit(temp[i]," ")[[1]]
for(k in 1:length(temp05)){
if(temp05[k]!=""){
gunma_matrix02[i,j]<- temp05[k]
j<- j+1
}
}
}
gunma_matrix03<- gunma_matrix02[,c(2,4,3,5)]
colnames(gunma_matrix03)<- colnames(gunma_matrix)
gunma_matrix<- rbind(gunma_matrix,gunma_matrix03)
#あとは日付やデータタイプなど
gunma_matrix$確定日<- str_replace_all(gunma_matrix$確定日,"R2","2020") %>% ymd()
gunma_matrix<- gunma_matrix[is.na(gunma_matrix$確定日)==F,]
gunma_matrix$年代<- as.factor(gunma_matrix$年代)
gunma_matrix$性別<- as.factor(gunma_matrix$性別)
gunma<- transform(gunma_matrix,受診都道府県="群馬県")
gunma
},error=function(e){
gunma<- dummy
gunma$受診都道府県[1]<- "群馬県"
gunma
})
#埼玉:csvオープンデータ、URL変化あり、ファイルやデータのフォーマットが時々、変わるので要注意
saitama<- tryCatch({
#HTMLからURLリンクを抽出、".csv"で抜き出し、最後=最新のリンクだけピックアップ
saitama_html<- read_html(
"https://opendata.pref.saitama.lg.jp/data/dataset/covid19-jokyo")
saitama_link<- html_nodes(saitama_html,"a") %>% html_attr("href")
saitama_link2<- subset(saitama_link,str_detect(saitama_link,".csv"))
saitama<- read.csv(saitama_link2[length(saitama_link2)])[,c(2:5)]
saitama<- saitama[,c(1,4,2,3)]
colnames(saitama)<- c("確定日","居住都道府県","年代","性別")
#県DBでは「重複のため削除」とされており、データも残されていないため削除
saitama<- saitama[-711,]
#確定日まで「調査中」となることがあるので、前の日付で補完
for(i in 1:nrow(saitama)){
if(saitama$確定日[i]=="調査中"){
saitama$確定日[i]<- saitama$確定日[(i-1)]
}
}
# saitama$確定日<- paste0("2020年",saitama$確定日)
saitama$確定日<- ymd(saitama$確定日)
saitama<- transform(saitama,受診都道府県="埼玉県")
saitama
},error=function(e){
saitama<- dummy
saitama$受診都道府県[1]<- "埼玉県"
saitama
})
#茨城:stopcovidのJSONデータ取り込み→HTMLスクレイピング
ibaraki<- tryCatch({
# ibaraki_url<- "https://raw.githubusercontent.com/a01sa01to/covid19-ibaraki/development/data/data.json"
# ibaraki<- fromJSON(ibaraki_url)$patients$data[,c(2,5,3,4)]
# colnames(ibaraki)<- c("確定日","居住都道府県","年代","性別")
# ibaraki$確定日<- as.Date(ibaraki$確定日)
# ibaraki<- transform(ibaraki,受診都道府県="茨城県")
# ibaraki
#htmlの取得とテーブルの抽出
ibaraki_url<- "https://www.pref.ibaraki.jp/1saigai/2019-ncov/ichiran.html"
ibaraki_table<- (read_html(ibaraki_url) %>% html_nodes("table") %>%
html_table(header=T))[[1]][,c(1,4,2,3)]
colnames(ibaraki_table)<- c("確定日","居住都道府県","年代","性別")
#加工・整形
ibaraki_table$確定日<- paste0("2020/",ibaraki_table$確定日) %>% ymd()
ibaraki_table$性別<- paste0(ibaraki_table$性別,"性")
ibaraki<- transform(ibaraki_table,受診都道府県="茨城県")
ibaraki$年代<- as.factor(ibaraki$年代)
ibaraki$性別<- as.factor(ibaraki$性別)
ibaraki
},error=function(e){
ibaraki<- dummy
ibaraki$受診都道府県[1]<- "茨城県"
ibaraki
})
#千葉:stopcovidのJSONデータ取り込み→PDFからスクレイピング
chiba<- tryCatch({
# chiba_url<- "https://raw.githubusercontent.com/civictechzenchiba/covid19-chiba/development/data/data.json"
# chiba<- fromJSON(chiba_url)$patients$data[,c(1,3,4,5)]
# colnames(chiba)<- c("確定日","居住都道府県","年代","性別")
# chiba$確定日<- as.Date(chiba$確定日)
# chiba<- transform(chiba,受診都道府県="千葉県")
# chiba
#PDFのURLを取得
chiba_html<- read_html("https://www.pref.chiba.lg.jp/shippei/press/2019/ncov-index.html")
chiba_link<- html_nodes(chiba_html,"a") %>% html_attr("href")
chiba_url<- paste0("https://www.pref.chiba.lg.jp",
subset(chiba_link,str_detect(chiba_link,".pdf"))[1])
#PDFのテキスト情報とページ数を取得
chiba_text<- pdf_text(chiba_url)
chiba_page<- pdf_info(chiba_url)$pages
#とりあえず1行1要素としてベクトルにまとめる
chiba_table<- vector()
for(i in 1:chiba_page){
chiba_table<- c(chiba_table,str_split(chiba_text[i],"\r\n")[[1]])
}
#空行と冒頭を削除
chiba_table<- chiba_table[chiba_table!="" & !is.na(chiba_table) &
str_detect(chiba_table,"月") == T]
chiba_table<- chiba_table[2:length(chiba_table)]
#データ数(陽性者数)を取得し、空のデータフレームを用意してループへ
chiba_numdata<- length(chiba_table)
chiba_matrix<- data.frame(matrix(NA,nrow=chiba_numdata,ncol=4))
for(i in 1:chiba_numdata){
#各行中の空白で要素を区切って、空白を消す
temp<- str_split(chiba_table[i]," ")
temp<- temp[[1]]
temp<- temp[temp!=""]
#空欄はなさそうなので、データを各列へ
numtemp<- length(temp)
chiba_matrix[i,1:3]<- temp[2:4]
chiba_matrix[i,4]<- temp[numtemp]
}
#他県データと同じ並びに列を並び替えて列名を付与
chiba<- chiba_matrix[,c(4,3,1,2)]
colnames(chiba)<- c("確定日","居住都道府県","年代","性別")
#確定日データを日付データへ
chiba$確定日<- as.Date(ymd(paste0("2020年",chiba$確定日)))
#その他データ型を整理して受診都道府県データを加える
chiba$居住都道府県<- as.character(chiba$居住都道府県)
chiba$年代<- as.factor(chiba$年代)
chiba$性別<- as.factor(chiba$性別)
chiba<- transform(chiba,受診都道府県="千葉県")
chiba
},error=function(e){
chiba<- dummy
chiba$受診都道府県[1]<- "千葉県"
chiba
})
#東京:csvオープンデータ→stopcovidのJSONデータ取り込み
tokyo<- tryCatch({
tokyo<- read.csv(
"https://stopcovid19.metro.tokyo.lg.jp/data/130001_tokyo_covid19_patients.csv",
fileEncoding = "UTF-8-BOM")
tokyo<- tokyo[,c(3,5,8:10)]
colnames(tokyo)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
tokyo$確定日<- ymd(tokyo$確定日)
tokyo<- tokyo[,c(2:5,1)]
# tokyo_url<- "https://raw.githubusercontent.com/tokyo-metropolitan-gov/covid19/development/data/data.json"
# tokyo<- fromJSON(tokyo_url)$patients$data[,c(1,2,3,4)]
# colnames(tokyo)<- c("確定日","居住都道府県","年代","性別")
# tokyo$確定日<- as.Date(tokyo$確定日)
# tokyo<- transform(tokyo,受診都道府県="東京都")
tokyo
},error=function(e){
tokyo<- dummy
tokyo$受診都道府県[1]<- "東京都"
tokyo
})
#神奈川:csvオープンデータ取り込み
kanagawa<- tryCatch({
kanagawa<- read.csv(
"http://www.pref.kanagawa.jp/osirase/1369/data/csv/patient.csv")
colnames(kanagawa)<- c("確定日","居住都道府県","年代","性別")
kanagawa<- transform(kanagawa,受診都道府県="神奈川県")
kanagawa$確定日<- ymd(kanagawa$確定日)
kanagawa
},error=function(e){
kanagawa<- dummy
kanagawa$受診都道府県[1]<- "神奈川県"
kanagawa
})
#新潟:stopcovidのHTMLスクレイピング→県サイトのHTMLスクレイピング
niigata<- tryCatch({
# niigata_html<- read_html(
# "https://niigata.stopcovid19.jp/cards/attributes-of-confirmed-cases/")
# niigata<- html_table(niigata_html)[[1]][,1:4]
# colnames(niigata)<- c("確定日","居住都道府県","年代","性別")
# niigata$確定日<- paste0("2020/",niigata$確定日)
# niigata$確定日<- ymd(niigata$確定日)
# niigata<- transform(niigata,受診都道府県="新潟県")
# niigata
niigata_url<- "https://www.pref.niigata.lg.jp/site/shingata-corona/256362836.html"
niigata_html<- read_html(niigata_url) %>% html_nodes("table")
niigata_table<- niigata_html[1] %>% html_table(fill=T)
niigata<- niigata_table[[1]][,c(3,6,4,5)]
colnames(niigata)<- c("確定日","居住都道府県","年代","性別")
niigata$確定日<- paste0("2020年",str_sub(niigata$確定日,start=1,end=-6)) %>% convfunc()
niigata$確定日<- str_remove_all(niigata$確定日,"\n") %>% ymd() %>% as.Date()
niigata$居住都道府県<- str_sub(niigata$居住都道府県,start=1,
end=regexpr("\n",niigata$居住都道府県)) %>%
str_remove_all("\n")
niigata$年代<- str_sub(niigata$年代,start=1,end=regexpr("\n",niigata$年代)) %>%
str_remove_all("\n") %>%
as.factor()
niigata$性別<- as.factor(niigata$性別)
niigata<- transform(niigata,受診都道府県="新潟県")
niigata
},error=function(e){
niigata<- dummy
niigata$受診都道府県[1]<- "新潟県"
niigata
})
#長野:stopcovidのHTMLスクレイピング→県サイトのPDFスクレイピング
nagano<- tryCatch({
# nagano_html<- read_html(
# "https://nagano.stopcovid19.jp/cards/attributes-of-confirmed-cases/")
# nagano<- html_table(nagano_html)[[1]][,c(1:4)]
# colnames(nagano)<- c("確定日","居住都道府県","年代","性別")
# nagano$確定日<- paste0("2020/",nagano$確定日)
# nagano$確定日<- ymd(nagano$確定日)
# nagano<- transform(nagano,受診都道府県="長野県")
# nagano
#PDFのURLを取得
# nagano_url<- "https://www.pref.nagano.lg.jp/hoken-shippei/kenko/kenko/kansensho/joho/corona-doko.html"
# nagano_html<- read_html(nagano_url) %>% html_nodes("a") %>% html_attr("href")
# nagano_link<- paste0("https://www.pref.nagano.lg.jp/",
# subset(nagano_html,str_detect(nagano_html,"situation"))[1])
#PDFのテキスト情報とページ数を抽出
# nagano_text<- pdf_text(nagano_link)
# nagano_page<- pdf_info(nagano_link)$pages
# nagano_table<- vector()
#ループでスペースで区切り、ベクトルに
# for(i in 1:nagano_page){
# nagano_table<- c(nagano_table,str_split(nagano_text[i],"\r\n")[[1]])
# }
#陽性者情報以外を削除
# nagano_table<- subset(nagano_table,str_detect(nagano_table,"(患者|無症状病原体保有者)")==T)
# nagano_table<- subset(nagano_table,str_detect(nagano_table,"滞在歴等")==F)
#陽性者数を把握、器を作成
# nagano_num<- length(nagano_table)
# nagano_matrix<- data.frame(matrix(NA,ncol=4,nrow=nagano_num))
# colnames(nagano_matrix)<- c("確定日","居住都道府県","年代","性別")
#各行をスペースで区切り、必要な情報を器に合わせて並べて整形
# for(i in 1:nagano_num){
# temp<- str_sub(nagano_table[i],start=regexpr("(患者|無症状病原体保有者)",
# nagano_table[i]),end=-1)
# temp<- str_split(temp," ")[[1]]
# temp<- temp[temp!=""]
# if(i < 14){
# temp<- temp[c(2,5,3,4)]
# }else{
# temp<- temp[c(2,6,3,4)]
# }
# nagano_matrix[i,]<- temp
# }
nagano_url<- "https://www.pref.nagano.lg.jp/hoken-shippei/kenko/kenko/kansensho/joho/corona-doko.html"
nagano_link<- read_html(nagano_url) %>% html_nodes("a") %>% html_attr("href")
nagano_link<- nagano_link[str_detect(nagano_link,".csv") &
!is.na(str_detect(nagano_link,".csv"))][1]
nagano_link<- paste0("https://www.pref.nagano.lg.jp",nagano_link)
nagano_matrix<- read.csv(nagano_link,
header=T,skip=1)[,c(5,7,8,9)]
colnames(nagano_matrix)<- c("確定日","居住都道府県","年代","性別")
#あとは日付やデータタイプなど
# nagano_matrix$確定日<- paste0("2020年",nagano_matrix$確定日) %>% ymd()
nagano_matrix$確定日<- ymd(nagano_matrix$確定日)
nagano_matrix$年代<- as.factor(nagano_matrix$年代)
nagano_matrix$性別<- as.factor(nagano_matrix$性別)
nagano<- transform(nagano_matrix,受診都道府県="長野県")
nagano
},error=function(e){
nagano<- dummy
nagano$受診都道府県[1]<- "長野県"
nagano
})
#山梨:stopcovidのJSONデータ取り込み
yamanashi<- tryCatch({
yamanashi_url<- "https://raw.githubusercontent.com/covid19-yamanashi/covid19/development/data/data.json"
yamanashi<- fromJSON(yamanashi_url)$patients$data[,c(5,3,1,2)]
colnames(yamanashi)<- c("確定日","居住都道府県","年代","性別")
yamanashi$確定日<- as.Date(yamanashi$確定日)
yamanashi<- transform(yamanashi,受診都道府県="山梨県")
yamanashi
},error=function(e){
yamanashi<- dummy
yamanashi$受診都道府県[1]<- "山梨県"
yamanashi
})
#富山:stopcovidのHTMLスクレイピング→xlsxオープンデータ
toyama<- tryCatch({
# toyama_html<- read_html(
# "https://stopcovid19-toyama.netlify.app/cards/attributes-of-confirmed-cases/")
# toyama<- html_table(toyama_html)[[1]][,2:5]
# colnames(toyama)<- c("確定日","居住都道府県","年代","性別")
# toyama$確定日<- paste0("2020/",toyama$確定日)
# toyama$確定日<- ymd(toyama$確定日)
# toyama<- transform(toyama,受診都道府県="富山県")
# toyama
#URL取得
toyama_url<- "http://www.pref.toyama.jp/cms_sec/1205/kj00021798.html"
toyama_html<- read_html(toyama_url) %>% html_nodes("a") %>% html_attr("href")
toyama_link<- toyama_html[str_detect(toyama_html,".xlsx")==T]
toyama_link<- toyama_link[is.na(toyama_link)==F]
#xlsxファイルからの読み込み
toyama<- read.xlsx(toyama_link,sheet=1,startRow=3)[,c(3,6,4,5)]
colnames(toyama)<- c("確定日","居住都道府県","年代","性別")
toyama$確定日<- as.Date(toyama$確定日,origin="1899-12-30")
toyama$年代<- as.factor(toyama$年代)
toyama$性別<- as.factor(toyama$性別)
toyama<- transform(toyama,受診都道府県="富山県")
toyama
},error=function(e){
toyama<- dummy
toyama$受診都道府県[1]<- "富山県"
toyama
})
#石川:stopcovidのHTMLスクレイピング→県サイトcsv読み込み
ishikawa<- tryCatch({
# ishikawa_html<- read_html(
# "https://ishikawa-covid19.netlify.app/cards/attributes-of-confirmed-cases/")
# ishikawa<- html_table(ishikawa_html)[[1]][,1:4]
# colnames(ishikawa)<- c("確定日","居住都道府県","年代","性別")
# ishikawa$確定日<- paste0("2020/",ishikawa$確定日)
# ishikawa$確定日<- ymd(ishikawa$確定日)
# ishikawa<- transform(ishikawa,受診都道府県="石川県")
# ishikawa
#URL取得
ishikawa_link<- "https://www.pref.ishikawa.lg.jp/kansen/coronakennai.html" %>%
read_html() %>% html_nodes("a") %>% html_attr("href")
ishikawa_url<- paste0("https://www.pref.ishikawa.lg.jp",
ishikawa_link[str_detect(ishikawa_link,"covid19_patients.csv")==T])
#csv読み込み
ishikawa<- read.csv(ishikawa_url)[c(5:8,3)]
colnames(ishikawa)<- c("確定日","居住都道府県","年代","性別","受診都道府県")
ishikawa$確定日<- ymd(ishikawa$確定日)
ishikawa$居住都道府県<- as.character(ishikawa$居住都道府県)
ishikawa
},error=function(e){
ishikawa<- dummy
ishikawa$受診都道府県[1]<- "石川県"
ishikawa
})
#福井:stopcovidのHTMLスクレイピング→csvオープンデータ
fukui<- tryCatch({
# fukui_html<- read_html(
# "https://covid19-fukui.com/cards/attributes-of-confirmed-cases/")
# fukui<- html_table(fukui_html)[[1]][,c(1,3:5)]
# colnames(fukui)<- c("確定日","居住都道府県","年代","性別")
# fukui$確定日<- paste0("2020/",fukui$確定日)
# fukui$確定日<- ymd(fukui$確定日)
# fukui<- transform(fukui,受診都道府県="福井県")
# fukui
#URL取得
fukui_link<- "https://www.pref.fukui.lg.jp/doc/toukei-jouhou/covid-19.html" %>%
read_html() %>% html_nodes("a") %>% html_attr("href")
fukui_url<- paste0("https://www.pref.fukui.lg.jp/doc/toukei-jouhou",
str_sub(fukui_link[is.na(fukui_link)==F &
str_detect(fukui_link,"patients.csv")==T],
start=2,end=-1))
#csv読み込み
fukui<- read.csv(fukui_url[1],fileEncoding="UTF-8-BOM")[c(5,7:9,3)]
colnames(fukui)<- c("確定日","居住都道府県","年代","性別","受診都道府県")
fukui$確定日<- ymd(fukui$確定日)
fukui$居住都道府県<- as.character(fukui$居住都道府県)
fukui
},error=function(e){
fukui<- dummy
fukui$受診都道府県[1]<- "福井県"
fukui
})
#岐阜:csvオープンデータ
gifu<- tryCatch({
gifu<- read.csv(
"https://gifu-opendata.pref.gifu.lg.jp/dataset/4661bf9d-6f75-43fb-9d59-f02eb84bb6e3/resource/9c35ee55-a140-4cd8-a266-a74edf60aa80/download/210005_gifu_covid19_patients.csv")
gifu<- gifu[,c(3,5,7:9)]
colnames(gifu)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
gifu$確定日<- ymd(gifu$確定日)
gifu<- gifu[,c(2:5,1)]
gifu
},error=function(e){
gifu<- dummy
gifu$受診都道府県[1]<- "岐阜県"
gifu
})
#静岡:stopcovidのHTMLスクレイピング→csvオープンデータ
shizuoka<- tryCatch({
# shizuoka_html<- read_html(
# "https://stopcovid19-shizuoka-dev.netlify.app/cards/attributes-of-confirmed-cases/")
# shizuoka<- html_table(shizuoka_html)[[1]][,c(1:4)]
# colnames(shizuoka)<- c("確定日","居住都道府県","年代","性別")
# shizuoka$確定日<- paste0("2020/",shizuoka$確定日)
# shizuoka$確定日<- ymd(shizuoka$確定日)
# shizuoka<- transform(shizuoka,受診都道府県="静岡県")
# shizuoka
#URLの取得
shizuoka_url<- "https://opendata.pref.shizuoka.jp/dataset/8167.html"
shizuoka_link<- (read_html(shizuoka_url) %>% html_nodes("a") %>% html_attr("href"))
shizuoka_link2<- paste0("https://opendata.pref.shizuoka.jp",
(shizuoka_link[str_detect(shizuoka_link,"patients")==T])[2])
#csvの読み込み、加工・整形
shizuoka<- read.csv(shizuoka_link2)[,c(5,7,8,9,3)]
colnames(shizuoka)<- c("確定日","居住都道府県","年代","性別","受診都道府県")
shizuoka$確定日<- ymd(shizuoka$確定日)
shizuoka$居住都道府県<- as.character(shizuoka$居住都道府県)
shizuoka
},error=function(e){
shizuoka<- dummy
shizuoka$受診都道府県[1]<- "静岡県"
shizuoka
})
#愛知:xlsxオープンデータ、URL変化あり →pdfのみとなったためスクレイピング
aichi<- tryCatch({
# aichi_html<- read_html("https://www.pref.aichi.jp/site/covid19-aichi/kansensya-kensa.html")
# aichi_link<- html_nodes(aichi_html,"a") %>% html_attr("href")
# aichi_url<- paste0("https://www.pref.aichi.jp",
# subset(aichi_link,str_detect(aichi_link,".xlsx")))
# aichi<- read.xlsx(aichi_url,
# startRow = 3)[,c(2,3,5)]
# colnames(aichi)<- c("確定日","属性","居住都道府県")
# aichi$確定日<- as.Date(aichi$確定日,origin="1899-12-30")
# aichi<- transform(aichi,年代=substr(aichi$属性,1,2))
# temp<- if_else(str_detect(aichi$属性,"男"),"男性","女性")
# aichi<- transform(aichi,性別=temp)
# aichi<- transform(aichi,受診都道府県="愛知県")
# aichi<- aichi[,c(1,3,4,5,6)]
# aichi
#PDFのURLを取得
aichi_html<- read_html("https://www.pref.aichi.jp/site/covid19-aichi/")
aichi_link<- html_nodes(aichi_html,"a") %>% html_attr("href")
aichi_url<- paste0("https://www.pref.aichi.jp",
subset(aichi_link,str_detect(aichi_link,".pdf")))
#PDFのテキスト情報とページ数を取得
aichi_text01<- pdf_text(aichi_url[7])
aichi_text02<- pdf_text(aichi_url[8])
# aichi_text<- c(aichi_text01,aichi_text02)
# aichi_page<- pdf_info(aichi_url)$pages
#とりあえず1行1要素としてベクトルにまとめる
aichi_table01<- vector()
for(i in 1:length(aichi_text01)){
if(i==1){
aichi_table01<- c(aichi_table01,str_split(aichi_text01[i],"\r\n")[[1]][4:46])
}else{
aichi_table01<- c(aichi_table01,str_split(aichi_text01[i],"\r\n")[[1]][2:46])
}
}
aichi_table02<- vector()
for(i in 1:length(aichi_text02)){
if(i==1){
aichi_table02<- c(aichi_table02,str_split(aichi_text02[i],"\r\n")[[1]][4:46])
}else{
aichi_table02<- c(aichi_table02,str_split(aichi_text02[i],"\r\n")[[1]][2:46])
}
}
#空行と冒頭を削除
aichi_table01<- aichi_table01[aichi_table01!="" & !is.na(aichi_table01)]
aichi_table01<- aichi_table01[4:length(aichi_table01)]
aichi_table02<- aichi_table02[aichi_table02!="" & !is.na(aichi_table02)]
aichi_table02<- aichi_table02[4:length(aichi_table02)]
aichi_table<- c(aichi_table01,aichi_table02)
aichi_table<- subset(aichi_table,(substr(aichi_table,1,1)!="※"))
#データ数(陽性者数)を取得し、空のデータフレームを用意してループへ
aichi_numdata<- length(aichi_table)
aichi_matrix<- data.frame(matrix(NA,nrow=aichi_numdata,ncol=7))
for(i in 1:aichi_numdata){
#各行中の空白で要素を区切る
temp<- str_split(aichi_table[i]," ")
temp<- temp[[1]]
#「国籍」とかいうおおよそ不要な(政治姿勢?)項目を削って要素数を取得
temp<- temp[temp!="" & temp!="日本" & temp!="中国" & temp!="外国籍"]
temp<- temp[1:5]
numtemp<- length(temp)
#各行3要素目に年代・性別が入っていればそのまま、入っていなければ空白にして整形
if(is.na(temp[3])==T){
temp[3]<- "NA"
}
if(str_detect(temp[3],"男性") ==T | str_detect(temp[3],"女性") ==T){
aichi_matrix[i,]<- c(temp,rep(NA,(7-numtemp)))
}else{
aichi_matrix[i,1:2]<- temp[1:2]
aichi_matrix[i,4:7]<- c(temp[3:numtemp],rep(NA,(6-numtemp)))
}
#3要素目の性別データに合わせて別に性別の列を作る
if(is.na(aichi_matrix[i,3])){
aichi_matrix[i,5]<- "不明"
}else if(str_detect(aichi_matrix[i,3],"男性")==T){
aichi_matrix[i,5]<- "男性"
}else if(str_detect(aichi_matrix[i,3],"女性")==T){
aichi_matrix[i,5]<- "女性"
}else{
aichi_matrix[i,5]<- "不明"
}
}
#3要素目に入っている性別を削除し、年代のみに
aichi_matrix[,3]<- str_remove_all(aichi_matrix[,3],"男性")
aichi_matrix[,3]<- str_remove_all(aichi_matrix[,3],"女性")
#他県データと同じ並びに列を並び替えて列名を付与
aichi<- aichi_matrix[,c(2,4,3,5)]
colnames(aichi)<- c("確定日","居住都道府県","年代","性別")
#確定日データを日付データへ
aichi<- subset(aichi,aichi$確定日!="欠番")
aichi$確定日<- as.Date(ymd(paste0("2020年",aichi$確定日)))
aichi<- subset(aichi,!(is.na(aichi$確定日)))
#その他データ型を整理して受診都道府県データを加える
aichi$居住都道府県<- as.character(aichi$居住都道府県)
aichi$年代<- as.factor(aichi$年代)
aichi$性別<- as.factor(aichi$性別)
aichi<- transform(aichi,受診都道府県="愛知県")
aichi
},error=function(e){
aichi<- dummy
aichi$受診都道府県[1]<- "愛知県"
aichi
},warning=function(w){
aichi<- dummy
aichi$受診都道府県[1]<- "愛知県"
aichi
})
#三重:csvオープンデータ→HTMLスクレイピング
mie<- tryCatch({
# mie<- read.csv(
# "https://www.pref.mie.lg.jp/common/content/000888623.csv")
# mie<- mie[,c(3,10,5,8:9)]
# colnames(mie)<- c("受診都道府県","居住都道府県","確定日","年代","性別")
# mie$確定日<- ymd(mie$確定日)
# mie<- mie[,c(3,2,4,5,1)]
# mie
#HTMLからテーブル要素を抽出、対象テーブルをテキスト化
# mie_url<- "https://www.pref.mie.lg.jp/YAKUMUS/HP/m0068000066_00002.htm"
# mie_node<- read_html(mie_url) %>% html_nodes("table")
# mie_target<- mie_node[1] %>% html_text()
#対象テーブルのテキストを区切りで分割、ベクトル化
# mie_text<- str_split(str_remove_all(mie_target,"\t"),"\r\n")[[1]]
# mie_text<- mie_text[7:length(mie_text)]
#月及び日をともに含む要素を抽出、全角を半角に変換
# mie_detect<- which(str_detect(mie_text,"月") & str_detect(mie_text,"日"))
# mie_date<- str_remove_all(paste0("2020/",mie_text[mie_detect])," ")
# mie_date<- gsub("月","/",mie_date) %>% str_remove_all("日")
# mie_date<- convfunc(mie_date)
#日付データを起点に、他のデータを抽出
# mie_resd<- mie_text[(mie_detect+1)]
# mie_age<- mie_text[(mie_detect+2)]
# mie_sex<- mie_text[(mie_detect+3)]
#データフレームにまとめて整形
# mie<- data.frame(cbind(mie_date,mie_resd,mie_age,mie_sex))
# colnames(mie)<- c("確定日","居住都道府県","年代","性別")
# mie$確定日<- as.Date(mie$確定日)
# mie$居住都道府県<- as.character(mie$居住都道府県)
# mie<- transform(mie,受診都道府県="三重県")
mie_url<- "https://www.pref.mie.lg.jp/YAKUMUS/HP/m0068000066_00002.htm"
mie_node<- read_html(mie_url) %>% html_nodes("a") %>% html_attr("href")
mie_link<- paste0("https://www.pref.mie.lg.jp/",
mie_node[str_detect(mie_node,"csv")==T &
!is.na(str_detect(mie_node,"csv"))][1]
)
mie_matrix<- read.csv(mie_link)[,c(5,8,9,10)]
colnames(mie_matrix)<- c("確定日","居住都道府県","年代","性別")
mie_matrix$確定日<- ymd(mie_matrix$確定日)
mie_matrix$居住都道府県<- as.character(mie_matrix$居住都道府県)
mie<- transform(mie_matrix,受診都道府県="三重県")
mie
},error=function(e){
mie<- dummy
mie$受診都道府県[1]<- "三重県"
mie
},warning=function(w){
mie<- dummy
mie$受診都道府県[1]<- "三重県"
mie
})
#滋賀:stopcovidのHTMLスクレイピング
#shiga_url<- "https://stopcovid19.pref.shiga.jp/cards/attributes-of-confirmed-cases/"
#shiga_html<- read_html(shiga_url)
#shiga_table<- html_nodes(shiga_html,"table") %>% html_table()
shiga<- tryCatch({
shiga_url<-
"https://raw.githubusercontent.com/Shiga-pref-org/covid19-data/gh-pages/data.json"
shiga<- fromJSON(shiga_url)$patients$data[,c(1:4)]
colnames(shiga)<- c("確定日","居住都道府県","年代","性別")
shiga$確定日<- as.Date(shiga$確定日)
shiga<- transform(shiga,受診都道府県="滋賀県")
shiga
},error=function(e){
shiga<- dummy
shiga$受診都道府県[1]<- "滋賀県"
shiga
})
#京都:stopcovidのJSONデータ取り込み→HTMLスクレイピング
kyoto<- tryCatch({
# kyoto_url<- "https://raw.githubusercontent.com/stopcovid19-kyoto/covid19/development/data/data.json"
# kyoto<- fromJSON(kyoto_url)$patients$data[,c(2,4,5,6)]
# colnames(kyoto)<- c("確定日","居住都道府県","年代","性別")
# kyoto$確定日<- as.Date(kyoto$確定日)
# kyoto<- transform(kyoto,受診都道府県="京都府")
# kyoto
# #html読み込み
# kyoto_url<- "http://www.pref.kyoto.jp/kentai/news/novelcoronavirus.html"
# kyoto_html<- read_html(kyoto_url)
#
# #親ページのテーブルを初期のテーブルにし、子ページへのリンクを取得
# kyoto_table<- html_nodes(kyoto_html,"table")[2] %>% html_table()
# kyoto_table<- kyoto_table[[1]]
# kyoto_link<- html_nodes(kyoto_html,"a") %>% html_attr("href")
# kyoto_link<- paste0("http://www.pref.kyoto.jp/",
# subset(kyoto_link,str_detect(kyoto_link,"corona/hassei")==T))
# kyoto_page<- length(kyoto_link)
#
# #子ページを巡回、テーブルにストック
# for(i in 1:kyoto_page){
# temp<- read_html(kyoto_link[i]) %>% html_nodes("table") %>% html_table()
# kyoto_table<- rbind(kyoto_table,temp[[1]])
# }
#
# #加工・整形
# kyoto<- kyoto_table[,c(2,5,3,4)]
# colnames(kyoto)<- c("確定日","居住都道府県","年代","性別")
# kyoto$確定日<- str_replace_all(kyoto$確定日,"令和2年","2020年") %>% ymd()
# kyoto$年代<- as.factor(kyoto$年代)
# kyoto$性別<- as.factor(kyoto$性別)
# kyoto<- transform(kyoto,受診都道府県="京都府")
# kyoto
#URL
kyoto_url<- "http://www.pref.kyoto.jp/kentai/corona/hassei1-50.html"
kyoto_html<- read_html(kyoto_url)
#get table
kyoto_table1<- html_nodes(kyoto_html,"table")[1] %>% html_table()
kyoto_table2<- html_nodes(kyoto_html,"table")[2] %>% html_table()
kyoto_table<- rbind(kyoto_table1[[1]],kyoto_table2[[1]])
#get pdf table
kyoto_texturl<- html_nodes(kyoto_html,"a") %>% html_attr("href")
kyoto_texturl<- subset(kyoto_texturl,str_detect(kyoto_texturl,".pdf"))
kyoto_texturl<- paste0("http://www.pref.kyoto.jp/",kyoto_texturl[length(kyoto_texturl)])
kyoto_text<- pdf_text(kyoto_texturl)
kyoto_table03<- vector()
for(i in 1:length(kyoto_text)){
temp01<- str_split(kyoto_text[i],"\r\n")[[1]]
if(i==1){
kyoto_table03<- c(kyoto_table03,temp01[3:(length(temp01)-1)])
}else{
kyoto_table03<- c(kyoto_table03,temp01[2:(length(temp01)-1)])
}
}
kyoto_table03<- kyoto_table03[is.na(as.integer(substr(kyoto_table03,1,1)))==F]
kyoto_ndata<- length(kyoto_table03)
kyoto_matrix<- data.frame(matrix(NA,nrow=kyoto_ndata,ncol=6))
for(i in 1:kyoto_ndata){
temp02<- strsplit(kyoto_table03[i]," ")[[1]]
k<- 1
for(j in 1:length(temp02)){
if(temp02[j]!=""){
kyoto_matrix[i,k]<- temp02[j]
k<- k+1
}
}
}
colnames(kyoto_matrix)<- colnames(kyoto_table)
kyoto_table<- rbind(kyoto_table,kyoto_matrix)
#reform table
kyoto_table<- kyoto_table[,c(2,5,3,4)]
kyoto_table[,1]<- str_replace_all(kyoto_table[,1],"令和2","2020")
colnames(kyoto_table)<- c("確定日","居住都道府県","年代","性別")
kyoto_table$確定日<- ymd(kyoto_table$確定日)
kyoto<- transform(kyoto_table,受診都道府県="京都府")
kyoto
},error=function(e){
kyoto<- dummy
kyoto$受診都道府県[1]<- "京都府"
kyoto
})
#大阪:stopcovidのJSONデータ取り込み
osaka<- tryCatch({
# osaka_url<- "https://raw.githubusercontent.com/codeforosaka/covid19/development/data/data.json"
# osaka<- fromJSON(osaka_url)$patients$data[,c(2,4,5,6)]
osaka01<- read.xlsx("http://www.pref.osaka.lg.jp/attach/23711/00346644/youseisyajyouhou.xlsx")
osaka01<- osaka01[2:nrow(osaka01),c(2,5,3,4)]
colnames(osaka01)<- c("確定日","居住都道府県","年代","性別")
osaka01$確定日<- as.Date(as.numeric(osaka01$確定日),origin="1899-12-30")
osaka02_url<- "http://www.pref.osaka.lg.jp/iryo/osakakansensho/happyo_kako.html"
osaka02_html<- read_html(osaka02_url)
osaka02_link<- html_nodes(osaka02_html,"a") %>% html_attr("href")
osaka02_link<- osaka02_link[str_detect(osaka02_link,".xlsx") &
!is.na(str_detect(osaka02_link,".xlsx"))]
osaka02_link<- paste0("http://www.pref.osaka.lg.jp",
osaka02_link[1:(length(osaka02_link)-14)])
osaka03_url<- "http://www.pref.osaka.lg.jp/iryo/osakakansensho/happyo.html"
osaka03_html<- read_html(osaka03_url)
osaka03_link<- html_nodes(osaka03_html,"a") %>% html_attr("href")
osaka03_link<- osaka03_link[str_detect(osaka03_link,".xlsx") &
!is.na(str_detect(osaka03_link,".xlsx"))]
osaka03_link<- paste0("http://www.pref.osaka.lg.jp",osaka03_link)
osaka02_link<- c(osaka02_link,osaka03_link)
osaka02_matrix<- data.frame(matrix(NA,ncol=4,nrow=1))
for(i in osaka02_link){
temp<- read.xlsx(i,sheet=2)
if(str_detect(i,"1116")==T){
temp02<- temp[10,]
temp02<- temp02[!is.na(temp02)]
temp02<- as.numeric(temp02)
colnames(temp02)<- NULL
}else{
temp02<- temp[9,]
temp02<- temp02[!is.na(temp02)]
temp02<- as.numeric(temp02)
colnames(temp02)<- NULL
}
temp_matrix<- data.frame(matrix(NA,nrow=sum(temp02),ncol=4))
k<- 1
for(j in 1:10){
if(j==1){
temp_matrix[k:(k+temp02[j]+temp02[j+1]-1),3]<- 0
k<- k + temp02[j]+temp02[j+1]
}else if(j==10){
temp_matrix[k:(k+temp02[j+1]+temp02[j+2]-1),3]<- 90
k<- k + temp02[j+1]+temp02[j+2]
}else{
temp_matrix[k:(k+temp02[j+1]),3]<- (j-1)*10
k<- k + temp02[j+1]
}
}
temp_date<- strsplit(i,"/")[[1]]
temp_date<- temp_date[length(temp_date)]
temp_date<- paste0("2020-",
substr(temp_date,1,2),
"-",
substr(temp_date,3,4))
temp_matrix[,1]<- temp_date
osaka02_matrix<- rbind(osaka02_matrix,temp_matrix)
}
osaka02_matrix<- osaka02_matrix[2:nrow(osaka02_matrix),]
colnames(osaka02_matrix)<- c("確定日","居住都道府県","年代","性別")
osaka<- rbind(osaka01,osaka02_matrix)
osaka$確定日<- as.Date(osaka$確定日)
osaka<- transform(osaka,受診都道府県="大阪府")
osaka
},error=function(e){
osaka<- dummy
osaka$受診都道府県[1]<- "大阪府"
osaka
})
#兵庫:stopcovidのJSONデータ取り込み→xlsxオープンデータ
hyogo<- tryCatch({
# hyogo_url<- "https://raw.githubusercontent.com/stop-covid19-hyogo/covid19/development/data/patients.json"
# hyogo<- fromJSON(hyogo_url)$data[,c(2,4,5,6)]
# colnames(hyogo)<- c("確定日","居住都道府県","年代","性別")
# hyogo$確定日<- as.Date(hyogo$確定日)
# hyogo<- transform(hyogo,受診都道府県="兵庫県")
# hyogo
#URLを取得
hyogo_link<- read_html("https://web.pref.hyogo.lg.jp/kk03/corona_hasseijyokyo.html") %>%
html_nodes("a") %>% html_attr("href")
hyogo_url<- paste0("https://web.pref.hyogo.lg.jp",
subset(hyogo_link,str_detect(hyogo_link,"kanjajokyou.xlsx")))
#xlsx読み込み、加工、整形
hyogo<- read.xlsx(hyogo_url,startRow=6,colNames=F)[,c(2,6,3,4)]
colnames(hyogo)<- c("確定日","居住都道府県","年代","性別")
hyogo$確定日<- as.numeric(hyogo$確定日)
hyogo<- hyogo[!is.na(hyogo$確定日),]
hyogo$確定日<- as.Date(hyogo$確定日,origin="1899-12-30")
hyogo$年代<- as.factor(hyogo$年代)
hyogo$性別<- as.factor(hyogo$性別)
hyogo<- transform(hyogo,受診都道府県="兵庫県")
hyogo
},error=function(e){
hyogo<- dummy
hyogo$受診都道府県<- "兵庫県"
hyogo
})
#和歌山:stopcovidのJSONデータ取り込み
wakayama<- tryCatch({
wakayama_url<- "https://raw.githubusercontent.com/sys-cube/covid19/master/data/data.json"
wakayama<- fromJSON(wakayama_url)$patients$data[,c(1:4)]
colnames(wakayama)<- c("確定日","居住都道府県","年代","性別")
wakayama$確定日<- as.Date(wakayama$確定日)
wakayama<- transform(wakayama,受診都道府県="和歌山県")
wakayama
},error=function(e){
wakayama<- dummy
wakayama$受診都道府県[1]<- "和歌山県"
wakayama
})
#奈良:xlsxオープンデータ
nara<- tryCatch({
nara_url<- "http://www.pref.nara.jp/secure/227193/%E5%A5%88%E8%89%AF%E7%9C%8C_01%E6%96%B0%E5%9E%8B%E3%82%B3%E3%83%AD%E3%83%8A%E3%82%A6%E3%82%A4%E3%83%AB%E3%82%B9%E6%84%9F%E6%9F%93%E8%80%85_%E6%82%A3%E8%80%85%E3%83%AA%E3%82%B9%E3%83%88.xlsx"
nara<- read.xlsx(nara_url,startRow = 2)[,c(3,4,6,7,8)]
colnames(nara)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
nara$確定日<- as.Date(nara$確定日,origin="1899-12-30")
nara<- nara[,c(2,3,4,5,1)]
nara
},error=function(e){
nara<- dummy
nara$受診都道府県[1]<- "奈良県"
nara
})
#鳥取:HTMLスクレイピング
tottori<- tryCatch({
#文字バイトの関係でHTMLをrawデータで取り込み
# tottori_res <- GET("https://www.pref.tottori.lg.jp/item/1207285.htm#itemid1207285")
# tottori_res2 <- content(tottori_res, "raw")
# tottori_node <- read_html(tottori_res2[tottori_res2 != 0]) %>% html_nodes("table")
# tottori_table <- html_table(tottori_node[3], fill=T,header = T)[[1]]
#テーブル要素を抽出、データ整形
tottori_url<- "https://www.pref.tottori.lg.jp/item/1207285.htm#itemid1207285"
tottori_html<- read_html(tottori_url) %>% html_nodes("table")
tottori_table<- tottori_html[3] %>% html_table()
tottori_table<- tottori_table[[1]]
tottori<- tottori_table[5:nrow(tottori_table),3:6]
colnames(tottori)<- c("確定日","居住都道府県","年代","性別")
tottori$確定日<- paste0("2020/",tottori$確定日)
tottori$確定日<- as.Date(ymd(tottori$確定日))
tottori$年代<- as.factor(tottori$年代)
tottori<- transform(tottori,受診都道府県="鳥取県")
tottori
},error=function(e){
tottori<- dummy
tottori$受診都道府県<- "鳥取県"
tottori
})
#島根:HTMLスクレイピング
shimane<- tryCatch({
#URLの設定、HTMLノードの取得
shimane_url<- "https://www.pref.shimane.lg.jp/medical/yakuji/kansensyo/other/topics/bukan2020.html"
shimane_html<- read_html(shimane_url) %>% html_nodes("p")
#tableではないため、該当しそうなノードのみ抽出し、テキスト情報のみに
shimane_table<- subset(shimane_html,str_detect(shimane_html,
'span style="letter-spacing'))
shimane_table<- shimane_table[2:length(shimane_table)]
#数字を半角に、陽性者数を取得
shimane_text<- html_text(shimane_table) %>% convfunc()
shimane_num<- length(shimane_text)
#各行中、冒頭の番号を削除
for(i in 1:shimane_num){
shimane_text[i]<- str_remove(shimane_text[i],as.character(i))
}
#データの入れ物を作成
shimane<- data.frame(matrix(NA,ncol=4,nrow=shimane_num))
colnames(shimane)<- c("確定日","居住都道府県","年代","性別")
#日付データは冒頭から「日」までを取得し、日付形式に変換
shimane$確定日<- paste0("2020年",
str_sub(shimane_text,start=1,end=(regexpr("日",shimane_text)))) %>%
str_remove_all(" ") %>% ymd()
#居住地データは性別末尾からベクトルの終わりまでを取得
shimane$居住都道府県<- str_sub(shimane_text,
start=(regexpr("(性|児)",shimane_text)+1)) %>%
str_remove_all("退院")
#年代データは「日」のあとの2文字(「不明」等もありうるため)
shimane$年代<- str_sub(shimane_text,start=(regexpr("日",shimane_text)+1),
end=(regexpr("日",shimane_text)+2))
shimane$年代<- as.factor(shimane$年代)
#性別データは「男」「女」の有無で判定(「不明」等もありうるため)
for(i in 1:shimane_num){
if(str_detect(shimane_text[i],"男")==T){
shimane$性別[i]<- "男性"
}else if(str_detect(shimane_text[i],"女")==T){
shimane$性別[i]<- "女性"
}else{
shimane$性別[i]<- "不明"
}
}
shimane$性別<- as.factor(shimane$性別)
#不要なデータを削除
shimane<- shimane[str_detect(shimane$性別,"性")==T |
is.na(shimane$年代)==T,]
#受診都道府県を加えて出力
shimane<- transform(shimane,受診都道府県="島根県")
shimane
},error=function(e){
shimane<- dummy
shimane$受診都道府県[1]<- "島根県"
shimane
})
#岡山:csvオープンデータ
okayama<- tryCatch({
okayama<- read.csv(
"http://www.okayama-opendata.jp/ckan/dataset/e6b3c1d2-2f1f-4735-b36e-e45d36d94761/resource/c6503ebc-b2e9-414c-aae7-7374f4801e21/download/kansenshashousaijouhou0420.csv")
okayama<- okayama[,c(2,4,6:8)]
colnames(okayama)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
okayama$確定日<- paste0("2020年",okayama$確定日)
okayama$確定日<- ymd(okayama$確定日)
okayama<- okayama[,c(2:5,1)]
okayama
},error=function(e){
okayama<- dummy
okayama$受診都道府県[1]<- "岡山県"
okayama
})
#山口:csvオープンデータ
yamaguchi<- tryCatch({
yamaguchi<- read.csv("https://yamaguchi-opendata.jp/ckan/dataset/f6e5cff9-ae43-4cd9-a398-085187277edf/resource/f56e6552-4c5d-4ec6-91c0-090f553e0aea/download/350001_yamaguchi_covid19_patients.csv"
,fileEncoding = "UTF-8-BOM")
yamaguchi<- yamaguchi[,c(5,4,7,8,3)]
colnames(yamaguchi)<- c("確定日","居住都道府県","年代","性別","受診都道府県")
yamaguchi$確定日<- as.Date(ymd(yamaguchi$確定日))
yamaguchi
},error=function(e){
yamaguchi<- dummy
yamaguchi$受診都道府県[1]<- "山口県"
yamaguchi
})
#徳島:HRMLスクレイピング
tokushima<- tryCatch({
tokushima_url<- "https://www.pref.tokushima.lg.jp/ippannokata/kenko/kansensho/5034012#20"
tokushima_html<- read_html(tokushima_url)
tokushima_table<- html_nodes(tokushima_html,"table")
tokushima1<- html_table(tokushima_table[6])[[1]][,2:4]
tokushima2<- html_table(tokushima_table[7])[[1]][,2:4]
tokushima<- rbind(tokushima1,tokushima2)
colnames(tokushima)<- c("確定日","年代","性別")
tokushima$確定日<- paste0("2020年",tokushima$確定日)
tokushima$確定日<- ymd(tokushima$確定日)
tokushima<- transform(tokushima,居住都道府県=NA)
tokushima<- transform(tokushima,受診都道府県="徳島県")
tokushima<- tokushima[,c(1,4,2,3,5)]
tokushima
},error=function(e){
tokushima<- dummy
tokushima$受診都道府県[1]<- "徳島県"
tokushima
})
#香川:HTMLスクレイピング、ただし、html_tableではうまく読めないので力技
kagawa<- tryCatch({
#HTMLのうち、ターゲットとなるテーブルのデータをテキストで取得
kagawa_url<- "https://www.pref.kagawa.lg.jp/content/etc/subsite/kansenshoujouhou/kansen/se9si9200517102553.shtml"
kagawa_html<- read_html(kagawa_url) %>% html_nodes("table")
kagawa_table<- kagawa_html[4]
kagawa_text<- html_text(kagawa_table)
#テキストデータを区切り文字で分割、スペース及び一番右端のデータを削除
kagawa_spltext<- str_split(kagawa_text,"\r\n")[[1]]
kagawa_data<- kagawa_spltext[kagawa_spltext != "" &
str_detect(kagawa_spltext,"報道") != T]
#データ長と陽性者数(元のテーブルの行数)を取得
kagawa_data<- kagawa_data[2:length(kagawa_data)]
kagawa_dnum<- length(kagawa_data)
kagawa_num<- as.numeric(kagawa_data) %>% max(na.rm=T)
#データの器を作成、行列の初期値を設定
kagawa_matrix<- data.frame(matrix(NA,ncol=5,nrow=kagawa_num))
j<-1
k<-2
temptime<- NULL
for(i in 1:kagawa_dnum){
options(warm=1)
#データがもしNoだったら一番左に格納
if(is.na(as.numeric(kagawa_data[i]))==F){
kagawa_matrix[j,1]<- kagawa_data[i]
j<- j+1
k<- 2
next
#データがもし日付だったら左から2番目に格納
}else if(str_detect(kagawa_data[i],"月")==T &
str_detect(kagawa_data[i],"日")==T){
temptime<- str_split(kagawa_data[i],"(")[[1]][1]
kagawa_matrix[(j-1),k]<- temptime
#k<- k+1
next
#もしその行の日付データがまだ埋まってなかったら前回の日付を格納、その隣にこのデータを格納
}else if(is.na(kagawa_matrix[(j-1),2])==T){
kagawa_matrix[(j-1),2]<- temptime
kagawa_matrix[(j-1),3]<- kagawa_data[i]
k<- k+1
next
#その他の場合は5列目まで順次格納
}else if(k < 6){
k<- k+1
kagawa_matrix[(j-1),k]<- kagawa_data[i]
}
}
options(warm=0)
#他に合わせて整形
kagawa<- kagawa_matrix[,c(2,5,3,4)]
colnames(kagawa)<- c("確定日","居住都道府県","年代","性別")
kagawa$確定日<- ymd(paste0("2020年",kagawa$確定日))
kagawa$年代<- as.factor(kagawa$年代)
kagawa$性別<- as.factor(kagawa$性別)
kagawa<- transform(kagawa,受診都道府県="香川県")
kagawa
},error=function(e){
kagawa<- dummy
kagawa$受診都道府県<- "香川県"
kagawa
})
#愛媛:csvオープンデータ
ehime<- tryCatch({
ehime<- read.csv(
"https://www.pref.ehime.jp/opendata-catalog/dataset/2174/resource/7068/380008_ehime_covid19_patients.csv",
fileEncoding = "UTF-8-BOM")
ehime<- ehime[,c(3,5,8:10)]
colnames(ehime)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
ehime$確定日<- ymd(ehime$確定日)
ehime<- ehime[,c(2:5,1)]
ehime
},error=function(e){
ehime<- dummy
ehime$受診都道府県[1]<- "愛媛県"
ehime
})
#高知:stopcovidのHTMLスクレイピング→csvオープンデータ
kouchi<- tryCatch({
# kouchi_html<- read_html(
# "https://stopcovid19.code4kochi.com/cards/attributes-of-confirmed-cases")
# kouchi<- html_table(kouchi_html)[[1]][,c(1:4)]
# colnames(kouchi)<- c("確定日","居住都道府県","年代","性別")
# kouchi$確定日<- paste0("2020/",kouchi$確定日)
# kouchi$確定日<- ymd(kouchi$確定日)
# kouchi<- transform(kouchi,受診都道府県="高知県")
# kouchi
#URL取得
kouchi_link<- read_html("https://www.pref.kochi.lg.jp/soshiki/111301/2020041300141.html") %>%
html_nodes("a") %>% html_attr("href")
kouchi_url<- paste0("https://www.pref.kochi.lg.jp/soshiki/111301/",
kouchi_link[str_detect(kouchi_link,".patients")==T])
#csv読み込み、加工整形
kouchi<- read.csv(kouchi_url)[,c(5,8,9,10,3)]
colnames(kouchi)<- c("確定日","居住都道府県","年代","性別","受診都道府県")
kouchi$確定日<- ymd(kouchi$確定日)
kouchi$居住都道府県<- as.character(kouchi$居住都道府県)
kouchi
},error=function(e){
kouchi<- dummy
kouchi$受診都道府県[1]<- "高知県"
kouchi
})
#福岡:csvオープンデータ
fukuoka<- tryCatch({
fukuoka<- read.csv(
"https://ckan.open-governmentdata.org/dataset/8a9688c2-7b9f-4347-ad6e-de3b339ef740/resource/c27769a2-8634-47aa-9714-7e21c4038dd4/download/400009_pref_fukuoka_covid19_patients.csv",
fileEncoding = "UTF-8-BOM")
fukuoka<- fukuoka[,c(3,5,8:10)]
colnames(fukuoka)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
fukuoka$確定日<- ymd(fukuoka$確定日)
fukuoka<- fukuoka[,c(2:5,1)]
fukuoka
},error=function(e){
fukuoka<- dummy
fukuoka$受診都道府県[1]<- "福岡県"
fukuoka
})
#佐賀:stopcovidのHTMLスクレイピング
saga<- tryCatch({
# saga_html<- read_html(
# "https://stopcovid19.code4saga.org/cards/attributes-of-confirmed-cases/")
# saga<- html_table(saga_html)[[1]][,c(1:4)]
# colnames(saga)<- c("確定日","居住都道府県","年代","性別")
# saga$確定日<- paste0("2020/",saga$確定日)
# saga$確定日<- ymd(saga$確定日)
# saga<- transform(saga,受診都道府県="佐賀県")
# saga
saga_url<- "https://www.pref.saga.lg.jp/kiji00373220/index.html"
saga_html<- read_html(saga_url) %>% html_nodes("table")
saga_table<- (saga_html[4] %>%
html_table())[[1]][,c("結果判明日","在住地等","年代","性別")]
colnames(saga_table)<- c("確定日","居住都道府県","年代","性別")
saga_table$確定日<- str_sub(saga_table$確定日,start=1,end=-6)
for(i in 1:length(saga_table$確定日)){
if(saga_table$確定日[i]==""){
saga_table$確定日[i]<- saga_table$確定日[i-1]
}
}
saga_table$確定日<- paste0("2020年",saga_table$確定日) %>% ymd()
saga_table$年代<- str_sub(saga_table$年代,
start=1,
end=regexpr("歳",saga_table$年代)-1)
saga<- transform(saga_table,受診都道府県="佐賀県")
saga
},error=function(e){
saga<- dummy
saga$受診都道府県[1]<- "佐賀県"
saga
})
#長崎:csvオープンデータ
nagasaki<- tryCatch({
nagasaki<- read.csv("http://data.bodik.jp/dataset/09951e04-dc5d-42e9-9a49-37443be6787e/resource/de7ce61e-1849-47a1-b758-bca3f809cdf8/download/20200428prefnagasakicovidpatients.csv",
fileEncoding = "UTF-8-BOM")[,c(5,8,9,10,3)]
colnames(nagasaki)<- c("確定日","居住都道府県","年代","性別","受診都道府県")
nagasaki$確定日<- ymd(nagasaki$確定日)
nagasaki
},error=function(e){
nagasaki<- dummy
nagasaki$受診都道府県<- "長崎県"
nagasaki
})
#熊本:stopcovidのJSONデータ取り込み
kumamoto<- tryCatch({
# kumamoto_temp<- read_html("https://kumamoto.stopcovid19.jp/cards/attributes-of-confirmed-cases/") %>%
# html_nodes("table") %>%
# html_table()
# kumamoto_url<- "https://raw.githubusercontent.com/codeforkumamoto/covid19/development/data/data.json"
# kumamoto<- fromJSON(kumamoto_url)$patients$data[,c(2,4,5,6)]
# kumamoto_url<- "https://kumamoto.stopcovid19.jp/cards/attributes-of-confirmed-cases/"
# kumamoto_html<- read_html(kumamoto_url)
# kumamoto_table<- html_nodes(kumamoto_html,"table")
kumamoto_url<- "https://www.pref.kumamoto.jp/common/UploadFileOutput.ashx?c_id=3&id=22038&sub_id=259&flid=258999"
kumamoto_table<- fread(kumamoto_url,encoding="UTF-8")[,c(5,7,8,9)]
kumamoto<- as.data.frame(kumamoto_table)
colnames(kumamoto)<- c("確定日","居住都道府県","年代","性別")
kumamoto$確定日<- as.Date(kumamoto$確定日)
kumamoto<- transform(kumamoto,受診都道府県="熊本県")
kumamoto
},error=function(e){
kumamoto<- dummy
kumamoto$受診都道府県[1]<- "熊本県"
kumamoto
})
#大分:csvオープンデータ
oita<- tryCatch({
oita<- read.csv(
"https://data.bodik.jp/dataset/f632f467-716c-46aa-8838-0d535f98b291/resource/3714d264-70f3-4518-a57a-8391e0851d7d/download/440001oitacovid19patients.csv",
fileEncoding = "UTF-8-BOM")
oita<- oita[,c(3,5,8:10)]
colnames(oita)<- c("受診都道府県","確定日","居住都道府県","年代","性別")
oita$確定日<- ymd(oita$確定日)
oita<- oita[,c(2:5,1)]
oita
},error=function(e){
oita<- dummy
oita$受診都道府県[1]<- "大分県"
oita
})
#宮崎:PDFからスクレイピング
miyazaki<- tryCatch({
# #PDFのURLを取得
# miyazaki_html<- read_html("https://www.pref.miyazaki.lg.jp/kenko/hoken/kansensho/covid19/hassei.html")
# miyazaki_link<- html_nodes(miyazaki_html,"a") %>% html_attr("href")
# miyazaki_url<- paste0("https://www.pref.miyazaki.lg.jp",
# subset(miyazaki_link,str_detect(miyazaki_link,".pdf")))
# #PDFのテキスト情報とページ数を取得
# miyazaki_text<- pdf_text(miyazaki_url)
# miyazaki_page<- pdf_info(miyazaki_url)$pages
#とりあえず1行1要素としてベクトルにまとめる
# miyazaki_table<- vector()
# for(i in 2:miyazaki_page){
# miyazaki_table<- c(miyazaki_table,str_split(miyazaki_text[i],"\r\n")[[1]])
# }
#空行と冒頭を削除
# miyazaki_table<- miyazaki_table[miyazaki_table!="" & !is.na(miyazaki_table) &
# str_detect(miyazaki_table,"月") == T]
# miyazaki_table<- miyazaki_table[2:length(miyazaki_table)]
#データ数(陽性者数)を取得し、空のデータフレームを用意してループへ
# miyazaki_numdata<- length(miyazaki_table)
# miyazaki_matrix<- data.frame(matrix(NA,nrow=miyazaki_numdata,ncol=4))
# for(i in 1:miyazaki_numdata){
#各行中の空白で要素を区切って、空白を消す
# temp<- str_split(miyazaki_table[i]," ")
# temp<- temp[[1]]
# temp<- temp[temp!=""]
#月日が入った要素を列のスタートに
# temp_st<- min(which(str_detect(temp,"月")))
# miyazaki_matrix[i,]<- temp[temp_st:(temp_st+3)]
# }
#他県データと同じ並びに列を並び替えて列名を付与
# miyazaki<- miyazaki_matrix[,c(1,4,2,3)]
# colnames(miyazaki)<- c("確定日","居住都道府県","年代","性別")
#確定日データを日付データへ
# miyazaki$確定日<- as.Date(ymd(paste0("2020年",miyazaki$確定日)))
#その他データ型を整理して受診都道府県データを加える
# miyazaki$居住都道府県<- as.character(miyazaki$居住都道府県)
# miyazaki$年代<- as.factor(miyazaki$年代)
# miyazaki$性別<- as.factor(miyazaki$性別)
# miyazaki<- transform(miyazaki,受診都道府県="宮崎県")
# miyazaki
#url
miyazaki_url<- "https://www.pref.miyazaki.lg.jp/kansensho-taisaku/covid-19/hassei_list.html"
miyazaki_html<- read_html(miyazaki_url)
miyazaki_table<- html_nodes(miyazaki_html,"table") %>% html_table(fill=T)
miyazaki<- miyazaki_table[[1]][,c(2,5,3,4)]
colnames(miyazaki)<- c("確定日","居住都道府県","年代","性別")
miyazaki$確定日<- paste0("2020年",miyazaki$確定日)
miyazaki$確定日<- ymd(miyazaki$確定日)
miyazaki<- transform(miyazaki,受診都道府県="宮崎県")
miyazaki
},error=function(e){
miyazaki<- dummy
miyazaki$受診都道府県[1]<- "宮崎県"
miyazaki
})
#鹿児島:HTMLスクレイピング
kagoshima<- tryCatch({
kagoshima_url<- "https://www.pref.kagoshima.jp/ae06/kenko-fukushi/kenko-iryo/kansen/kansensho/coronavirus.html#kokunai"
kagoshima_html<- read_html(kagoshima_url) %>% html_nodes("table")
kagoshima<- kagoshima_html[5] %>% html_table(header=T,fill=T)
kagoshima<- kagoshima[[1]][,2:5]
colnames(kagoshima)<- c("確定日","居住都道府県","年代","性別")
kagoshima$確定日<- paste0("2020年",kagoshima$確定日) %>% ymd()
kagoshima<- transform(kagoshima,受診都道府県="鹿児島県")
kagoshima$年代<- as.factor(kagoshima$年代)
kagoshima$性別<- as.factor(kagoshima$性別)
#どうやらこのレコードだけ重複計上っぽいので削除
kagoshima<- kagoshima[kagoshima$居住都道府県!="大阪府吹田市",]
kagoshima
},error=function(e){
kagoshima<- dummy
kagoshima$受診都道府県[1]<- "鹿児島県"
kagoshima
})
#沖縄:stopcovidのHTMLスクレイピング
okinawa<- tryCatch({
# okinawa_html<- read_html(
# "https://okinawa.stopcovid19.jp/cards/attributes-of-confirmed-cases/")
# okinawa<- html_table(okinawa_html)[[1]][,c(1:4)]
okinawa_url<- "https://raw.githubusercontent.com/Code-for-OKINAWA/covid19/development/data/data.json"
okinawa<- fromJSON(okinawa_url)$patients$data[,c(1,2,3,4)]
colnames(okinawa)<- c("確定日","居住都道府県","年代","性別")
# okinawa$確定日<- paste0("2020/",okinawa$確定日)
okinawa$確定日<- as.Date(okinawa$確定日)
okinawa$確定日<- ymd(okinawa$確定日)
okinawa<- transform(okinawa,受診都道府県="沖縄県")
okinawa
},error=function(e){
okinawa<- dummy
okinawa$受診都道府県[1]<- "沖縄県"
okinawa
})
#データフレームとして結合
df<- rbind(
hokkaido,
aomori,
akita,
iwate,
miyagi,
yamagata,
fukushima,
tochigi,
gunma,
saitama,
ibaraki,
tokyo,
chiba,
kanagawa,
niigata,
toyama,
ishikawa,
fukui,
nagano,
yamanashi,
gifu,
shizuoka,
aichi,
mie,
shiga,
kyoto,
osaka,
wakayama,
nara,
hyogo,
tottori,
shimane,
okayama,
#hiroshima,
yamaguchi,
tokushima,
kagawa,
ehime,
kouchi,
fukuoka,
saga,
nagasaki,
kumamoto,
oita,
miyazaki,
kagoshima,
okinawa
)
df
}
#ジャッグジャパン社データ
jjapandb<- tryCatch({
# i<-1
# if(i==1){
# #最初にWeb上のcsvロードにチャレンジ
# jjapandb<- read.csv("https://dl.dropboxusercontent.com/s/6mztoeb6xf78g5w/COVID-19.csv"
# ,fileEncoding = "UTF-8-BOM"
# ,stringsAsFactors = T
# )[,c("通し","確定日","居住都道府県","年代","性別","受診都道府県","備考")]
# jjapandb<- jjapandb[is.na(jjapandb$通し)==F,]
# jjapandb<- jjapandb[,c("確定日","居住都道府県","年代","性別","受診都道府県","備考")]
# jjapandb
# }else{
# # が挟まれている行などの関係でcsvではうまく読み込めないため、レスポンスを取得
# jj_url<- "https://dl.dropboxusercontent.com/s/6mztoeb6xf78g5w/COVID-19.csv"
# jj_response<- GET(jj_url)
#解析・データ取得
# jj_content<- content(jj_response,as="parsed")
#空行を削除のうえ、整形
# jj_table<- jj_content[is.na(jj_content$通し)==F,] %>% data.frame()
# jjapandb<- jj_table[,c(8,11,6,7,10,19)]
# jjapandb
# }
jjapandb<- fread("https://dl.dropboxusercontent.com/s/6mztoeb6xf78g5w/COVID-19.csv",
encoding="UTF-8")
jjapandb<- as.data.frame(jjapandb)
jjapandb<- jjapandb[,c(1,8,11,6,7,10,19)]
colnames(jjapandb)<- c("通し","確定日","居住都道府県","年代","性別","受診都道府県","備考")
jjapandb<- jjapandb[is.na(jjapandb$通し)==F,]
jjapandb<- jjapandb[,c("確定日","居住都道府県","年代","性別","受診都道府県","備考")]
jjapandb
},error=function(e){
#エラー判定が出れば、あらかじめダウンロードしていたcsvに切り替え
jjapandb<- tryCatch({
jjapandb<- read.csv("C:/Users/user/Downloads/COVID-19.csv"
,stringsAsFactors = T
)[,c("確定日","居住都道府県","年代","性別","受診都道府県","備考")]
jjapandb
},error=function(e){
#それでも無理なら空のダミー(以下、警告の場合も同様)
jjapandb<- dummy_jj
jjapandb$受診都道府県[1]<- "東京都"
jjapandb
},warning=function(w){
jjapandb<- dummy_jj
jjapandb$受診都道府県[1]<- "東京都"
jjapandb
})
#},warning=function(w){
# jjapandb<- tryCatch({
# jjapandb<- read.csv("C:/Users/user/Downloads/COVID-19.csv"
# ,stringsAsFactors = T
# )[,c("確定日","居住都道府県","年代","性別","受診都道府県","備考")]
# jjapandb
# },error=function(e){
# jjapandb<- dummy_jj
# jjapandb$受診都道府県[1]<- "東京都"
# jjapandb
# },warning=function(w){
# jjapandb<- dummy_jj
# jjapandb$受診都道府県[1]<- "東京都"
# jjapandb
# })
})
#データタイプの整合性確保、長崎クルーズ船の別カテゴリ作成
jjapandb$受診都道府県<- as.character(jjapandb$受診都道府県)
jjapandb$備考<- as.character(jjapandb$備考)
#jjapandb[is.na(jjapandb$備考),]$備考<- "NA"
jjapandb$受診都道府県<- if_else(jjapandb$受診都道府県 == "長崎県" &
str_detect(jjapandb$備考,"クルーズ") ==T,
"長崎客船",jjapandb$受診都道府県)
jjapandb$受診都道府県<- as.factor(jjapandb$受診都道府県)
jjapandb<- jjapandb[,c(1,2,3,4,5)]
jjapandb$居住都道府県<- as.character(jjapandb$居住都道府県)
#空行を削除し、整形
jjapandb<- subset(jjapandb,!(is.na(jjapandb$確定日)))
colnames(jjapandb)<- c("確定日","居住都道府県","年代","性別","受診都道府県")
jjapandb$確定日<- mdy(jjapandb$確定日)
jjapandb$確定日<- as.Date(jjapandb$確定日)
#自治体・団体等のデータ取り込み
cordfordb<- getdata()
cordfordb<- cordfordb[!is.na(cordfordb$確定日),]
cordfordb$居住都道府県<- as.character(cordfordb$居住都道府県)
#都道府県別に集計し、判定のための行列を作成
jjapanflag<-
jjapandb %>%
group_by(受診都道府県) %>%
summarize(Number_of_Rows = n())
colnames(jjapanflag)<- c("id","jj")
cordforflag<-
cordfordb %>%
group_by(受診都道府県) %>%
summarize(Number_of_Rows = n())
colnames(cordforflag)<- c("id","cf")
#上記双方のデータを結合、各都道府県別にいずれが多いかを評価
checkflag<- merge(jjapanflag,cordforflag,by="id",all=T)
checkflag<- checkflag[checkflag$id!="東京都",]
checkflag$jj<- replace(checkflag$jj,which(is.na(checkflag$jj)),0)
checkflag$cf<- replace(checkflag$cf,which(is.na(checkflag$cf)),0)
checkflag<- transform(checkflag,check=checkflag$jj<=checkflag$cf)
#JJ社及び自治体・団体のいずれを優先するかをフラグ化
cordforget<- checkflag[checkflag$check==T,]$id
jjapanget<- checkflag[checkflag$check==F,]$id
#居住地データ関係で東京を暫定的に…
cordforget[(length(cordforget)+1)]<- "東京都"
#上記判定結果に基づいてそれぞれからデータを抽出、統合
if(length(jjapanget)==0){
df_cf<- cordfordb
df_cf<- transform(df_cf,source="各自治体・団体等分")
df<- df_cf
}else{
df_jj<- filter(jjapandb,受診都道府県 %in% jjapanget)
df_jj<- transform(df_jj,source="ジャッグジャパン社分")
df_cf<- filter(cordfordb,受診都道府県 %in% cordforget)
df_cf<- transform(df_cf,source="各自治体・団体等分")
df<- rbind(df_jj,df_cf)
}
#東京都の更新日データを判定・取得
cf_tokyo_date<- max(cordfordb[cordfordb$受診都道府県=="東京都",]$確定日)
jj_tokyo_date<- max(jjapandb[jjapandb$受診都道府県=="東京都",]$確定日)
if(jj_tokyo_date > cf_tokyo_date){
temp<- jjapandb[jjapandb$確定日==jj_tokyo_date &
jjapandb$受診都道府県=="東京都",]
temp<- transform(temp,source="ジャッグジャパン社分")
df<- rbind(df,temp)
}
df<- df[order(df$確定日),]
#--------------------------------------------------
#居住都道府県判定のためのデータ作成(なお、次のステップで参照データと突き合わせるための加工なのでかなり適当です)
df<- transform(df,居住地=NA)
df<- transform(df,参考_居住地元データ=df$居住都道府県)
n<- length(df$居住都道府県)
for(i in 1:n){
#空白なら「不明」
if(is.na(df$居住都道府県[i])){
df$居住地[i]<- "不明"
#「県」が入って入ればそこで切って県名に
}else if(str_detect(df$居住都道府県[i],"県")==T){
df$居住地[i]<- paste0(str_split(df$居住都道府県[i],"県")[[1]][1],"県")
#「北海道」「大阪府」「京都府」「東京都」が入っていればそれらに
}else if(str_detect(df$居住都道府県[i],"北海道")==T){
df$居住地[i]<- "北海道"
}else if(str_detect(df$居住都道府県[i],"大阪府")==T){
df$居住地[i]<- "大阪府"
}else if(str_detect(df$居住都道府県[i],"京都府")==T){
df$居住地[i]<- "京都府"
}else if(str_detect(df$居住都道府県[i],"東京都")==T){
df$居住地[i]<- "東京都"
}else if(str_detect(df$居住都道府県[i],"都内")==T){
df$居住地[i]<- "東京都"
#「市」が入っていれば県名+当該市に
}else if(str_detect(df$居住都道府県[i],"市")==T &
str_detect(df$居住都道府県[i],"町")==F){
df$居住地[i]<- paste0(df$受診都道府県[i],paste0(
str_split(df$居住都道府県[i],"市")[[1]][1],"市"))
#「町」が入っていれば県名+当該町に
}else if(str_detect(df$居住都道府県[i],"町")==T &
str_detect(df$居住都道府県[i],"市")==F){
df$居住地[i]<- paste0(df$受診都道府県[i],paste0(
str_split(df$居住都道府県[i],"町")[[1]][1],"町"))
}else{
df$居住地[i]<- df$居住都道府県[i]
}
}
#--------------------------------------------------
#整形のうえ、出力
colnames(df)<- c("確定日","参照元","年代","性別","受診都道府県","source","居住地","参考居住地元")
df<- df[,c(1,3,4,5,7,8,6)]
#可能な範囲である程度までクレンジング
df<- transform(df,参考性別元=df$性別)
df<- transform(df,参考年代元=df$年代)
#年代から余計な文字を削除、全角を半角に
df$年代<- str_remove_all(df$年代,"(代|歳|未満|以上)")
df$年代<- convfunc(df$年代)
df$年代<- str_remove_all(df$年代,"[^0-9]")
df$年代<- as.numeric(df$年代)
df$年代<- as.character(df$年代)
df$年代<- if_else(is.na(df$年代),"NA",df$年代)
df$年代<- recode(df$年代,"1"="0","2"="0","18"="10","100"="90","715"="NA","86"="80","817"="NA","816"="NA","814"="NA","89"="80","820"="NA","827"="NA","910"="NA",
"107"="NA","418"="NA","424"="NA","112"="NA")
#性別区分
#df$性別<- str_remove_all(df$性別,"(性|児|?| )")
#df$性別<- str_replace_all(df$性別,"男","男性") %>%
# str_replace_all("女","女性")
#df$性別[str_detect(df$性別,"男") == F &
# str_detect(df$性別,"女") == F] <-"不明"
df$性別[str_detect(df$性別,"男")==T]<- "男性"
df$性別[str_detect(df$性別,"女")==T]<- "女性"
df$性別[str_detect(df$性別,"男")==F &
str_detect(df$性別,"女")==F]<- "NA"
df$性別[is.na(df$性別)]<- "NA"
write.csv(checkflag,"check_pref_scrayping_result.csv")
write.csv(df,"covid-19_confirmed_cases.csv")
df