123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- #!/usr/bin/env Rscript
- library(tidyverse)
- library(lubridate)
- library(padr)
- 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()
- ))
-
- c <- complete %>%
- filter(ano_cat == 2012) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2012-01-01"),
- end_val = ymd("2012-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes)
- c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/br_mes_dia.csv_2012", delim = ";")
- # 2013
- c <- complete %>%
- filter(ano_cat == 2013) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2013-01-01"),
- end_val = ymd("2013-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes)
- c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/br_mes_dia_2013.csv", delim = ";")
- #2014
- c <- complete %>%
- filter(ano_cat == 2014) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2014-01-01"),
- end_val = ymd("2014-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes)
- c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/br_mes_dia_2014.csv", delim = ";")
- #2015
- c <- complete %>%
- filter(ano_cat == 2015) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2015-01-01"),
- end_val = ymd("2015-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes)
- c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/br_mes_dia_2015.csv", delim = ";")
- #2016
- c <- complete %>%
- filter(ano_cat == 2016) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2016-01-01"),
- end_val = ymd("2016-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes)
- c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/br_mes_dia_2016.csv", delim = ";")
- #2017
- c <- complete %>%
- filter(ano_cat == 2017) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2017-01-01"),
- end_val = ymd("2017-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes)
- c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/br_mes_dia_2017.csv", delim = ";")
- # Filter: By all states of Brazil and by year
- # 2012
- c <- complete %>%
- filter(ano_cat == 2012) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2012-01-01"),
- end_val = ymd("2012-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes) %>%
- group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/estados_mes_dia_2012.csv", delim = ";")
- # 2013
- c <- complete %>%
- filter(ano_cat == 2013) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2013-01-01"),
- end_val = ymd("2013-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes) %>%
- group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/estados_mes_dia_2013.csv", delim = ";")
- # 2014
- c <- complete %>%
- filter(ano_cat == 2014) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2014-01-01"),
- end_val = ymd("2014-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes) %>%
- group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/estados_mes_dia_2014.csv", delim = ";")
- # 2015
- c <- complete %>%
- filter(ano_cat == 2015) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2015-01-01"),
- end_val = ymd("2015-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes) %>%
- group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/estados_mes_dia_2015.csv", delim = ";")
- # 2016
- c <- complete %>%
- filter(ano_cat == 2016) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2016-01-01"),
- end_val = ymd("2016-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes) %>%
- group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/estados_mes_dia_2016.csv", delim = ";")
- # 2017
- c <- complete %>%
- filter(ano_cat == 2017) %>%
- select(uf, dt_acidente) %>%
- group_by(uf, dt_acidente) %>%
- summarize(acidentes = n()) %>%
- pad(start_val = ymd("2017-01-01"),
- end_val = ymd("2017-12-31"),
- break_above = 2) %>%
- fill_by_value(acidentes, 0) %>%
- mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
- select(uf, mes, dia, acidentes) %>%
- group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
- write_delim(c, "../app/data/heatmap/estados_mes_dia_2017.csv", delim = ";")
|