time-summary.R 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. library(tidyverse)
  2. complete <- read_csv2("../app/data/completo.csv", na = "NA",
  3. col_types = cols(
  4. pais = col_character(),
  5. regiao = col_character(),
  6. uf = col_character(),
  7. mesorregiao = col_character(),
  8. microrregiao = col_character(),
  9. municipio = col_character(),
  10. st_acidente_feriado = col_character(),
  11. ds_agente_causador = col_character(),
  12. ano_cat = col_integer(),
  13. ds_cnae_classe_cat = col_character(),
  14. dt_acidente = col_date(),
  15. st_dia_semana_acidente = col_character(),
  16. ds_emitente_cat = col_character(),
  17. hora_acidente = col_time(),
  18. idade_cat = col_integer(),
  19. cd_indica_obito = col_character(),
  20. ds_natureza_lesao = col_character(),
  21. ds_cbo = col_character(),
  22. ds_parte_corpo_atingida = col_character(),
  23. cd_tipo_sexo_empregado_cat = col_character(),
  24. ds_tipo_acidente = col_character(),
  25. ds_tipo_local_acidente = col_character()
  26. ))
  27. estimativa_pop <- read_csv2("../app/data/estimativas.csv", na = "NA",
  28. col_types = cols(
  29. uf = col_character(),
  30. municipio = col_character(),
  31. populacao = col_integer(),
  32. ano = col_integer()
  33. ))
  34. estimativa_pop <- rename(estimativa_pop, ano_cat = ano)
  35. #Summarization of the number of accidents occurred by year 2012-2016
  36. ac_mun_2012 <- complete %>%
  37. group_by(uf, municipio, ano_cat) %>%
  38. filter(ano_cat == 2012) %>%
  39. summarize(acidentes = n())
  40. ac_mun_2013 <- complete %>%
  41. group_by(uf, municipio, ano_cat) %>%
  42. filter(ano_cat == 2013) %>%
  43. summarize(acidentes = n())
  44. ac_mun_2014 <- complete %>%
  45. group_by(uf, municipio, ano_cat) %>%
  46. filter(ano_cat == 2014) %>%
  47. summarize(acidentes = n())
  48. ac_mun_2015 <- complete %>%
  49. group_by(uf, municipio, ano_cat) %>%
  50. filter(ano_cat == 2015) %>%
  51. summarize(acidentes = n())
  52. ac_mun_2016 <- complete %>%
  53. group_by(uf, municipio, ano_cat) %>%
  54. filter(ano_cat == 2016) %>%
  55. summarize(acidentes = n())
  56. ac_mun <- rbind(ac_mun_2012, ac_mun_2013, ac_mun_2014, ac_mun_2015, ac_mun_2016)
  57. est_mun <- estimativa_pop %>% filter(ano_cat < 2017)
  58. write_delim(ac_mun_2012, "ac_mun_2012.csv", delim = ";")
  59. write_delim(ac_mun_2013, "ac_mun_2013.csv", delim = ";")
  60. write_delim(ac_mun_2014, "ac_mun_2014.csv", delim = ";")
  61. write_delim(ac_mun_2015, "ac_mun_2015.csv", delim = ";")
  62. write_delim(ac_mun_2016, "ac_mun_2016.csv", delim = ";")
  63. acidentes <- estimativa_pop %>% inner_join(ac_mun, by = c("uf", "municipio", "ano_cat"))
  64. ac <- acidentes %>% mutate(porcentagem = round((acidentes/populacao) * 100, 4))