123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #!/usr/bin/env Rscript
- library(tidyverse)
- library(lubridate)
- library(padr)
- print("Loading complete data file...")
- complete <- read_csv2("../app/data/completo.csv", na = "NA",
- col_types = cols(
- pais = col_character(),
- regiao = col_character(),
- uf = col_character(),
- mesorregiao = col_character(),
- microrregiao = col_character(),
- municipio = col_character(),
- st_acidente_feriado = col_character(),
- ds_agente_causador = col_character(),
- ano_cat = col_integer(),
- ds_cnae_classe_cat = col_character(),
- dt_acidente = col_date(),
- st_dia_semana_acidente = col_character(),
- ds_emitente_cat = col_character(),
- hora_acidente = col_time(),
- idade_cat = col_integer(),
- cd_indica_obito = col_character(),
- ds_natureza_lesao = col_character(),
- ds_cbo = col_character(),
- ds_parte_corpo_atingida = col_character(),
- cd_tipo_sexo_empregado_cat = col_character(),
- ds_tipo_acidente = col_character(),
- ds_tipo_local_acidente = col_character()
- ))
- print("Loading estimation of population files...")
- est2012 <- read_csv2("../app/data/estimativa_municipio_2012.csv",
- na = "NA",
- col_types = cols(
- uf = col_character(),
- municipio = col_character(),
- populacao = col_integer(),
- ano = col_integer()
- ))
- est2013 <- read_csv2("../app/data/estimativa_municipio_2013.csv",
- na = "NA",
- col_types = cols(
- uf = col_character(),
- municipio = col_character(),
- populacao = col_integer(),
- ano = col_integer()
- ))
- est2014 <- read_csv2("../app/data/estimativa_municipio_2014.csv",
- na = "NA",
- col_types = cols(
- uf = col_character(),
- municipio = col_character(),
- populacao = col_integer(),
- ano = col_integer()
- ))
- est2015 <- read_csv2("../app/data/estimativa_municipio_2015.csv",
- na = "NA",
- col_types = cols(
- uf = col_character(),
- municipio = col_character(),
- populacao = col_integer(),
- ano = col_integer()
- ))
- est2016 <- read_csv2("../app/data/estimativa_municipio_2016.csv",
- na = "NA",
- col_types = cols(
- uf = col_character(),
- municipio = col_character(),
- populacao = col_integer(),
- ano = col_integer()
- ))
- est2017 <- read_csv2("../app/data/estimativa_municipio_2017.csv",
- na = "NA",
- col_types = cols(
- uf = col_character(),
- municipio = col_character(),
- populacao = col_integer(),
- ano = col_integer()
- ))
- #Summarise population
- print("Summarising population...")
- est12 <- est2012 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
- est13 <- est2013 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
- est14 <- est2014 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
- est15 <- est2015 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
- est16 <- est2016 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
- est17 <- est2017 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
- print("Summarising accidents...")
- acd12 <- complete %>% filter(ano_cat == 2012) %>%
- group_by(uf) %>%
- summarise(acidentes = n())
- acd13 <- complete %>% filter(ano_cat == 2013) %>%
- group_by(uf) %>%
- summarise(acidentes = n())
- acd14 <- complete %>% filter(ano_cat == 2014) %>%
- group_by(uf) %>%
- summarise(acidentes = n())
- acd15 <- complete %>% filter(ano_cat == 2015) %>%
- group_by(uf) %>%
- summarise(acidentes = n())
- acd16 <- complete %>% filter(ano_cat == 2016) %>%
- group_by(uf) %>%
- summarise(acidentes = n())
- acd17 <- complete %>% filter(ano_cat == 2017) %>%
- group_by(uf) %>%
- summarise(acidentes = n())
- #Join estimations tables and c
- print("Joining tables...")
- percent_UF_est12 <- inner_join(acd12, est12, by = c("uf")) %>%
- select(uf, acidentes, populacao) %>%
- group_by(uf) %>%
- summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
- mutate(percentage = (acidentes/populacao) * 100)
- percent_UF_est13 <- inner_join(acd13, est13, by = c("uf")) %>%
- select(uf, acidentes, populacao) %>%
- group_by(uf) %>%
- summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
- mutate(percentage = (acidentes/populacao) * 100)
- percent_UF_est14 <- inner_join(acd14, est14, by = c("uf")) %>%
- select(uf, acidentes, populacao) %>%
- group_by(uf) %>%
- summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
- mutate(percentage = (acidentes/populacao) * 100)
- percent_UF_est15 <- inner_join(acd15, est15, by = c("uf")) %>%
- select(uf, acidentes, populacao) %>%
- group_by(uf) %>%
- summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
- mutate(percentage = (acidentes/populacao) * 100)
- percent_UF_est16 <- inner_join(acd16, est16, by = c("uf")) %>%
- select(uf, acidentes, populacao) %>%
- group_by(uf) %>%
- summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
- mutate(percentage = (acidentes/populacao) * 100)
- percent_UF_est17 <- inner_join(acd17, est17, by = c("uf")) %>%
- select(uf, acidentes, populacao) %>%
- group_by(uf) %>%
- summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
- mutate(percentage = (acidentes/populacao) * 100)
- print("Writing csv files...")
- write_delim(percent_UF_est12, "../app/data/percentage_UF2012.csv", delim = ";")
- write_delim(percent_UF_est13, "../app/data/percentage_UF2013.csv", delim = ";")
- write_delim(percent_UF_est14, "../app/data/percentage_UF2014.csv", delim = ";")
- write_delim(percent_UF_est15, "../app/data/percentage_UF2015.csv", delim = ";")
- write_delim(percent_UF_est16, "../app/data/percentage_UF2016.csv", delim = ";")
- write_delim(percent_UF_est17, "../app/data/percentage_UF2017.csv", delim = ";")
- print("Writing rds file...")
- saveRDS(percent_UF_est12, "../app/data/rds/percentage_UF2012.rds")
- saveRDS(percent_UF_est13, "../app/data/rds/percentage_UF2013.rds")
- saveRDS(percent_UF_est14, "../app/data/rds/percentage_UF2014.rds")
- saveRDS(percent_UF_est15, "../app/data/rds/percentage_UF2015.rds")
- saveRDS(percent_UF_est16, "../app/data/rds/percentage_UF2016.rds")
- saveRDS(percent_UF_est17, "../app/data/rds/percentage_UF2017.rds")
- print("Finishing gracefully!")
|