
```{r data}
theUrl = 'https://www.datosabiertos.gob.pe/sites/default/files/datos_abiertos_vigilancia_edas_2000_2023.csv'
theFile = file.path(normalizePath("~/Downloads"), basename(theUrl))
if(!file.exists(theFile)) download.file(theUrl, theFile)
deaths = read.csv(theFile)

peru = geodata::gadm("PER", level=3, path=normalizePath("~/Downloads"))

peru$name =paste(toupper(peru$NAME_1), toupper(peru$NAME_2), toupper(peru$NAME_3), sep='_')
deaths$name =paste(toupper(deaths$departamento), toupper(deaths$provincia), toupper(deaths$distrito), sep='_')

deaths = deaths[grep("DATEM DEL", deaths$name, invert=TRUE), ]


accentmatrix =matrix(c(
"^LIMA_", "LIMA PROVINCE_",
"-", " ", "[.]", " ", "Ѐ", "E", "Ñ", "N", "Á", "A", "Í","I", "É","E", "Ú","U", "Ó","O",
"�", "N"
),
	 ncol=2, byrow=TRUE)

for(D in 1:nrow(accentmatrix)) {
	peru$name = gsub(accentmatrix[D,1], accentmatrix[D,2], peru$name)
	deaths$name = gsub(accentmatrix[D,1], accentmatrix[D,2], deaths$name)
}



deaths$name = gsub("RAIMONDI", "RAYMONDI", deaths$name)
deaths$name = gsub("MILPUCC", "MILPUC", deaths$name)
deaths$name = gsub("IHUAYLLO","HUAYLLO",deaths$name)
deaths$name = gsub("HUAILLATI","HUAYLLATI",deaths$name)


deaths$name = gsub("PAMPAS GRANDE","PAMPAS", deaths$name)
deaths$name = gsub("MARISCAL GAMARRA","GAMARRA", deaths$name)

deaths$name = gsub("HUALLAY.GRANDE", "HUAYLLAY GRANDE", deaths$name)
deaths$name = gsub("AYMARAES_SANA", "AYMARAES_SANAYCA", deaths$name)

deaths$name = gsub("RITA DE SIHUAS","RITA DE SIGUAS", deaths$name)


deaths$name = gsub("FAJARDO_HUALLA", "AJARDO_HUAYA", deaths$name)

deaths$name = gsub("KIMBIRI","QUIMBIRI", deaths$name)

deaths$name = gsub("HUAROCHIRI_CASTA", "HUAROCHIRI_SAN PEDRO DE CASTA", deaths$name)
deaths$name = gsub("MAGDALENA DEL MAR","MAGDALENA VIEJA", deaths$name)



deaths$name = gsub("HUANUCO_HUANUCO", "HUANUCO_HUENUCO", deaths$name)
deaths$name = gsub("CA[EЀ]+ETE", "CANETE", deaths$name)
deaths$name = gsub("SAN PEDRO DE LARAOS", "LARAOS", deaths$name)



accentmatrix =matrix(c("CAYALTI","CHICLAYO",
	 "MARAEON", "MARANON",
"COCHABAMBA","COLCABAMBA", "CALLERIA","CALLARIA",
"LA TINGUI(NA)+","LA TINGUI", 
"DE YESO", "DEL YESO",
"ALLAUCA","AYAUCA",
"ALOMIAS","ALOMIA",  
"SAN PEDRO DE SANO", "SANO",
"CAEETE","CANETE",
"CHANCAYBANOS", "CHANCAYBA",
"_QUICHUAS", "_QUISHUAR",
"SANAYCAYCA","SANAYCA",
"_TRES DE ", "_3 DE "
),
	 ncol=2, byrow=TRUE)

for(D in 1:nrow(accentmatrix)) {
	peru$name = gsub(accentmatrix[D,1], accentmatrix[D,2], peru$name)
	deaths$name = gsub(accentmatrix[D,1], accentmatrix[D,2], deaths$name)
}


d1= sort(setdiff(deaths$name, peru$name))
d2 = sort(setdiff(peru$name, deaths$name))


length(d1);length(d2)
#d1
#d2

xx="DICIEMBRE"
grep(xx, peru$name, value=TRUE)  
unique(grep(xx, d1, value=TRUE)  )
#unique(grep(xx, d2, value=TRUE)  )
deaths2 = aggregate(deaths[,grep("defunciones|hospital", names(deaths))], deaths[,c('name','ano')], sum)


values(peru)[peru$name %in% d2,]
 sort(table(deaths$name[deaths$name %in% d1]), decreasing=TRUE)[1:30]  


peruWrap = wrap(peru)
```





