Last active
January 12, 2023 06:51
-
-
Save gabrielzanlorenssi/b4a9e74d77195a7d8c9cc6a741261e21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pacotes ----------------------------------------------------------------- | |
library(tidyverse) | |
library(jsonlite) | |
# Configurar -------------------------------------------------------------- | |
#-- codigo pleito | |
# pleito = 544 # 1t !! (usar esse para teste) !! | |
pleito = 545 # 2t !! (usar esse no dia 30/10) !! | |
#-- url | |
ufs <- str_to_lower(c("RO","AC", "AM", "RR", "PA", "AP", "TO", | |
"MA", "PI", "CE", "RN", "PB", "PE", "AL", "SE", "BA", | |
"MG", "ES", "RJ", "SP", | |
"PR", "SC", "RS", | |
"MS", "MT", "GO", "DF", | |
"ZZ")) | |
# Download ---------------------------------------------------------------- | |
pres_ufs <- map(ufs, function(x) { | |
url_uf = paste0("https://resultados.tse.jus.br/oficial/ele2022/", pleito, | |
"/dados-simplificados/", x, "/", x, | |
"-c0001-e000", pleito, "-r.json") | |
fromJSON(url_uf, simplifyDataFrame = TRUE) | |
}) | |
#-- resultados | |
res_uf <- imap_dfr(ufs, function(x,y) { | |
pres_ufs[[y]] %>% | |
.[["cand"]] %>% | |
tbl_df() %>% | |
mutate(voto=as.numeric(vap), | |
pct=voto/sum(voto, na.rm=T)) %>% | |
mutate(uf = str_to_upper(x)) | |
}) | |
eleit_ap_uf <- imap_dfr(ufs, function(x,y) { | |
pres_ufs[[y]] %>% | |
.[["ea"]] %>% | |
tibble() %>% set_names('ele_apurado') %>% | |
mutate(uf = str_to_upper(x))}) %>% | |
left_join(imap_dfr(ufs, function(x,y) { | |
pres_ufs[[y]] %>% | |
.[["e"]] %>% | |
tibble() %>% set_names('ele_total') %>% | |
mutate(uf = str_to_upper(x))}), by="uf") %>% | |
mutate(perc_ele_apurado = as.numeric(str_replace_all(ele_apurado, ",", "\\.")) / | |
as.numeric(str_replace_all(ele_total, ",", "\\."))) | |
# Analise ----------------------------------------------------------------- | |
## percentual | |
res_uf %>% | |
filter(!is.nan(pct)) %>% | |
select(n, voto, uf) %>% | |
mutate(uf = str_to_upper(uf)) %>% | |
group_by(uf) %>% | |
mutate(voto = round(voto*100/sum(voto, na.rm=T),1)) %>% | |
pivot_wider(names_from=n, values_from=voto) %>% | |
left_join(eleit_ap_uf, by="uf") %>% | |
mutate(perc_ele_apurado = as.numeric(str_replace_all(perc_ele_apurado, ",", "\\."))) -> ufs | |
ufs %>% | |
select(uf, `22`, `13`, perc_ele_apurado) %>% | |
mutate(perc_ele_apurado=round(perc_ele_apurado*100,1)) %>% | |
left_join(nexo.utils::infoState %>% | |
select(uf, region), by="uf")-> p | |
View(p) | |
## absoluto | |
# res_uf %>% | |
# filter(!is.nan(pct)) %>% | |
# select(n, voto, uf) %>% | |
# mutate(uf = str_to_upper(uf)) %>% | |
# pivot_wider(names_from=n, values_from=voto) %>% | |
# left_join(eleit_ap_uf, by="uf") %>% | |
# mutate(perc_ele_apurado = as.numeric(str_replace_all(perc_ele_apurado, ",", "\\."))) %>% | |
# mutate(votos_validos = `22`+`13`) -> ufs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment