123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- #!/usr/bin/env Rscript
- library(tidyverse)
- library(lubridate)
- library(padr)
- args = commandArgs(trailingOnly=TRUE)
- if (length(args) > 0) {
- file_path <- args[1]
- } else {
- file_path <- "../app/data/completo.csv"
- }
- complete <- read_csv2(file_path, 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()
- ))
- #Summarization of the number of accidents occurred by day, each year
- #2012
- ac_mun_2012 <- complete %>%
- group_by(uf, municipio, ano_cat) %>%
- filter(ano_cat == 2012) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2012-01-01"),
- end_val = ymd("2012-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(municipio, n)
- ac_micro_2012 <- complete %>%
- group_by(microrregiao, ano_cat) %>%
- filter(ano_cat == 2012) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2012-01-01"),
- end_val = ymd("2012-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(microrregiao, n)
- ac_meso_2012 <- complete %>%
- group_by(mesorregiao, ano_cat) %>%
- filter(ano_cat == 2012) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2012-01-01"),
- end_val = ymd("2012-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(mesorregiao, n)
- ac_uf_2012 <- complete %>%
- group_by(uf, ano_cat) %>%
- filter(ano_cat == 2012) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2012-01-01"),
- end_val = ymd("2012-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(uf, n)
- ac_regiao_2012 <- complete %>%
- group_by(regiao, ano_cat) %>%
- filter(ano_cat == 2012) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2012-01-01"),
- end_val = ymd("2012-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(regiao, n)
- ac_pais_2012 <- complete %>%
- group_by(pais, ano_cat) %>%
- filter(ano_cat == 2012) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2012-01-01"),
- end_val = ymd("2012-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(pais, n)
- #2013
- ac_mun_2013 <- complete %>%
- group_by(uf, municipio, ano_cat) %>%
- filter(ano_cat == 2013) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2013-01-01"),
- end_val = ymd("2013-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(municipio, n)
- ac_micro_2013 <- complete %>%
- group_by(microrregiao, ano_cat) %>%
- filter(ano_cat == 2013) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2013-01-01"),
- end_val = ymd("2013-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(microrregiao, n)
- ac_meso_2013 <- complete %>%
- group_by(mesorregiao, ano_cat) %>%
- filter(ano_cat == 2013) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2013-01-01"),
- end_val = ymd("2013-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(mesorregiao, n)
- ac_uf_2013 <- complete %>%
- group_by(uf, ano_cat) %>%
- filter(ano_cat == 2013) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2013-01-01"),
- end_val = ymd("2013-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(uf, n)
- ac_regiao_2013 <- complete %>%
- group_by(regiao, ano_cat) %>%
- filter(ano_cat == 2013) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2013-01-01"),
- end_val = ymd("2013-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(regiao, n)
- ac_pais_2013 <- complete %>%
- group_by(pais, ano_cat) %>%
- filter(ano_cat == 2013) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2013-01-01"),
- end_val = ymd("2013-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(pais, n)
- #2014
- ac_mun_2014 <- complete %>%
- group_by(uf, municipio, ano_cat) %>%
- filter(ano_cat == 2014) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2014-01-01"),
- end_val = ymd("2014-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(municipio, n)
- ac_micro_2014 <- complete %>%
- group_by(microrregiao, ano_cat) %>%
- filter(ano_cat == 2014) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2014-01-01"),
- end_val = ymd("2014-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(microrregiao, n)
- ac_meso_2014 <- complete %>%
- group_by(mesorregiao, ano_cat) %>%
- filter(ano_cat == 2014) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2014-01-01"),
- end_val = ymd("2014-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(mesorregiao, n)
- ac_uf_2014 <- complete %>%
- group_by(uf, ano_cat) %>%
- filter(ano_cat == 2014) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2014-01-01"),
- end_val = ymd("2014-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(uf, n)
- ac_regiao_2014 <- complete %>%
- group_by(regiao, ano_cat) %>%
- filter(ano_cat == 2014) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2014-01-01"),
- end_val = ymd("2014-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(regiao, n)
- ac_pais_2014 <- complete %>%
- group_by(pais, ano_cat) %>%
- filter(ano_cat == 2014) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2014-01-01"),
- end_val = ymd("2014-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(pais, n)
- #2015
- ac_mun_2015 <- complete %>%
- group_by(uf, municipio, ano_cat) %>%
- filter(ano_cat == 2015) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2015-01-01"),
- end_val = ymd("2015-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(municipio, n)
- ac_micro_2015 <- complete %>%
- group_by(microrregiao, ano_cat) %>%
- filter(ano_cat == 2015) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2015-01-01"),
- end_val = ymd("2015-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(microrregiao, n)
- ac_meso_2015 <- complete %>%
- group_by(mesorregiao, ano_cat) %>%
- filter(ano_cat == 2015) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2015-01-01"),
- end_val = ymd("2015-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(mesorregiao, n)
- ac_uf_2015 <- complete %>%
- group_by(uf, ano_cat) %>%
- filter(ano_cat == 2015) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2015-01-01"),
- end_val = ymd("2015-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(uf, n)
- ac_regiao_2015 <- complete %>%
- group_by(regiao, ano_cat) %>%
- filter(ano_cat == 2015) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2015-01-01"),
- end_val = ymd("2015-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(regiao, n)
- ac_pais_2015 <- complete %>%
- group_by(pais, ano_cat) %>%
- filter(ano_cat == 2015) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2015-01-01"),
- end_val = ymd("2015-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(pais, n)
- #2016
- ac_mun_2016 <- complete %>%
- group_by(uf, municipio, ano_cat) %>%
- filter(ano_cat == 2016) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2016-01-01"),
- end_val = ymd("2016-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(municipio, n)
- ac_micro_2016 <- complete %>%
- group_by(microrregiao, ano_cat) %>%
- filter(ano_cat == 2016) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2016-01-01"),
- end_val = ymd("2016-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(microrregiao, n)
- ac_meso_2016 <- complete %>%
- group_by(mesorregiao, ano_cat) %>%
- filter(ano_cat == 2016) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2016-01-01"),
- end_val = ymd("2016-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(mesorregiao, n)
- ac_uf_2016 <- complete %>%
- group_by(uf, ano_cat) %>%
- filter(ano_cat == 2016) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2016-01-01"),
- end_val = ymd("2016-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(uf, n)
- ac_regiao_2016 <- complete %>%
- group_by(regiao, ano_cat) %>%
- filter(ano_cat == 2016) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2016-01-01"),
- end_val = ymd("2016-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(regiao, n)
- ac_pais_2016 <- complete %>%
- group_by(pais, ano_cat) %>%
- filter(ano_cat == 2016) %>%
- count(dt_acidente) %>%
- pad(start_val = ymd("2016-01-01"),
- end_val = ymd("2016-12-31"),
- break_above = 2) %>%
- fill_by_value(n, 0) %>% ungroup() %>%
- select(pais, n)
- # Write all csvs
- # 2012
- write_delim(ac_mun_2012, "../app/data/barchart/ac_mun_2012.csv", delim = ";")
- write_delim(ac_micro_2012, "../app/data/barchart/ac_micro_2012.csv", delim = ";")
- write_delim(ac_meso_2012, "../app/data/barchart/ac_meso_2012.csv", delim = ";")
- write_delim(ac_uf_2012, "../app/data/barchart/ac_uf_2012.csv", delim = ";")
- write_delim(ac_regiao_2012, "../app/data/barchart/ac_regiao_2012.csv", delim = ";")
- write_delim(ac_pais_2012, "../app/data/barchart/ac_pais_2012.csv", delim = ";")
- # 2013
- write_delim(ac_mun_2013, "../app/data/barchart/ac_mun_2013.csv", delim = ";")
- write_delim(ac_micro_2013, "../app/data/barchart/ac_micro_2013.csv", delim = ";")
- write_delim(ac_meso_2013, "../app/data/barchart/ac_meso_2013.csv", delim = ";")
- write_delim(ac_uf_2013, "../app/data/barchart/ac_uf_2013.csv", delim = ";")
- write_delim(ac_regiao_2013, "../app/data/barchart/ac_regiao_2013.csv", delim = ";")
- write_delim(ac_pais_2013, "../app/data/barchart/ac_pais_2013.csv", delim = ";")
- # 2014
- write_delim(ac_mun_2014, "../app/data/barchart/ac_mun_2014.csv", delim = ";")
- write_delim(ac_micro_2014, "../app/data/barchart/ac_micro_2014.csv", delim = ";")
- write_delim(ac_meso_2014, "../app/data/barchart/ac_meso_2014.csv", delim = ";")
- write_delim(ac_uf_2014, "../app/data/barchart/ac_uf_2014.csv", delim = ";")
- write_delim(ac_regiao_2014, "../app/data/barchart/ac_regiao_2014.csv", delim = ";")
- write_delim(ac_pais_2014, "../app/data/barchart/ac_pais_2014.csv", delim = ";")
- # 2015
- write_delim(ac_mun_2015, "../app/data/barchart/ac_mun_2015.csv", delim = ";")
- write_delim(ac_micro_2015, "../app/data/barchart/ac_micro_2015.csv", delim = ";")
- write_delim(ac_meso_2015, "../app/data/barchart/ac_meso_2015.csv", delim = ";")
- write_delim(ac_uf_2015, "../app/data/barchart/ac_uf_2015.csv", delim = ";")
- write_delim(ac_regiao_2015, "../app/data/barchart/ac_regiao_2015.csv", delim = ";")
- write_delim(ac_pais_2015, "../app/data/barchart/ac_pais_2015.csv", delim = ";")
- # 2016
- write_delim(ac_mun_2016, "../app/data/barchart/ac_mun_2016.csv", delim = ";")
- write_delim(ac_micro_2016, "../app/data/barchart/ac_micro_2016.csv", delim = ";")
- write_delim(ac_meso_2016, "../app/data/barchart/ac_meso_2016.csv", delim = ";")
- write_delim(ac_uf_2016, "../app/data/barchart/ac_uf_2016.csv", delim = ";")
- write_delim(ac_regiao_2016, "../app/data/barchart/ac_regiao_2016.csv", delim = ";")
- write_delim(ac_pais_2016, "../app/data/barchart/ac_pais_2016.csv", delim = ";")
|