-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadSpain.R
41 lines (35 loc) · 1.69 KB
/
LoadSpain.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
LoadSpain <- function(){
#county geojson data
geomSPAIN <- st_read('https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/spain-provinces.geojson')
#Main COVID-19 hub page: https://cnecovid.isciii.es/covid19/#distribuci%C3%B3n-geogr%C3%A1fica
SPAIN<- read.csv("https://cnecovid.isciii.es/covid19/resources/casos_tecnica_provincia.csv",na.strings=FALSE)
#code link file
Spaincode = read.csv("./spain_codenames.csv",encoding="UTF-8",na.strings=FALSE)
#Population data comes from Instituto Nacional de Estadística: https://www.ine.es/jaxiT3/Datos.htm?t=2852#!tabs-tabla
DataJoin = c()
Counties <- unique(SPAIN$provincia_iso)
DataJoin$ProvinceName = Counties
for(aa in 1:length(Counties)){
Subset = SPAIN[SPAIN$provincia_iso==Counties[aa],]
Dates = as.Date(Subset$fecha)
LL = length(Dates)
ConfirmedCovidCases = cumsum(Subset$num_casos)
#CaseDiff = 10*( Subset$ConfirmedCovidCases[LL] - Subset$ConfirmedCovidCases[LL - 14])/ 14
CaseDiff = ConfirmedCovidCases[LL] - ConfirmedCovidCases[LL - 14]
#Make sure difference in cases is positive. If not set to NA.
if(CaseDiff<0){
CaseDiff = NA
}
DataJoin$LatestTime[aa] = as.character(Dates[LL])
DataJoin$CaseDiff[aa] = CaseDiff
}
Spaindata = as.data.frame(DataJoin)
Spaincode = as.data.frame(Spaincode)
Spaindf = inner_join(Spaindata,Spaincode,by=c("ProvinceName"="code"))
SpainMap <- inner_join(geomSPAIN,Spaindf, by = "name")
SpainMap$RegionName = paste0(SpainMap$name,", Spain")
SpainMap$DateReport = as.character(SpainMap$LatestTime)
SpainMap$pInf = SpainMap$CaseDiff/SpainMap$population2019
SPAIN_DATA = subset(SpainMap,select=c("DateReport","RegionName","pInf","geometry"))
return(SPAIN_DATA)
}